fmtlib: backport upstream fixes to suppress bogus gcc 13.1 warnings
[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>
58#include <lttng/location-internal.hpp>
59#include <lttng/lttng-error.h>
60#include <lttng/rotate-internal.hpp>
61#include <lttng/session-descriptor-internal.hpp>
62#include <lttng/session-internal.hpp>
63#include <lttng/tracker.h>
64#include <lttng/trigger/trigger-internal.hpp>
65#include <lttng/userspace-probe-internal.hpp>
2f77fc4b 66
28ab034a
JG
67#include <algorithm>
68#include <inttypes.h>
69#include <stdio.h>
70#include <sys/stat.h>
71#include <urcu/list.h>
72#include <urcu/uatomic.h>
2f77fc4b 73
a503e1ef
JG
74/* Sleep for 100ms between each check for the shm path's deletion. */
75#define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
76
d7bfb9b0
JG
77namespace lsu = lttng::sessiond::ust;
78
f1494934
JG
79static enum lttng_error_code wait_on_path(void *path);
80
81namespace {
3e3665b8
JG
82struct cmd_destroy_session_reply_context {
83 int reply_sock_fd;
84 bool implicit_rotation_on_destroy;
3285a971
JG
85 /*
86 * Indicates whether or not an error occurred while launching the
87 * destruction of a session.
88 */
89 enum lttng_error_code destruction_status;
3e3665b8
JG
90};
91
a503e1ef
JG
92/*
93 * Command completion handler that is used by the destroy command
94 * when a session that has a non-default shm_path is being destroyed.
95 *
96 * See comment in cmd_destroy_session() for the rationale.
97 */
f1494934 98struct destroy_completion_handler {
a503e1ef
JG
99 struct cmd_completion_handler handler;
100 char shm_path[member_sizeof(struct ltt_session, shm_path)];
101} destroy_completion_handler = {
28ab034a 102 .handler = { .run = wait_on_path, .data = destroy_completion_handler.shm_path },
a503e1ef
JG
103 .shm_path = { 0 },
104};
105
2f77fc4b
DG
106/*
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
d88aee68
DG
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
110 * held.
2f77fc4b 111 */
f1494934
JG
112pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
113uint64_t relayd_net_seq_idx;
114} /* namespace */
2f77fc4b 115
f1494934 116static struct cmd_completion_handler *current_completion_handler;
7076b56e
JG
117static int validate_ust_event_name(const char *);
118static int cmd_enable_event_internal(struct ltt_session *session,
28ab034a
JG
119 const struct lttng_domain *domain,
120 char *channel_name,
121 struct lttng_event *event,
122 char *filter_expression,
123 struct lttng_bytecode *filter,
124 struct lttng_event_exclusion *exclusion,
125 int wpipe);
126static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
127 const struct lttng_domain *domain,
128 const struct lttng_channel *_attr,
129 int wpipe);
7076b56e 130
2f77fc4b
DG
131/*
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
134 */
28ab034a 135static int build_network_session_path(char *dst, size_t size, struct ltt_session *session)
2f77fc4b
DG
136{
137 int ret, kdata_port, udata_port;
cd9adb8b 138 struct lttng_uri *kuri = nullptr, *uuri = nullptr, *uri = nullptr;
2f77fc4b
DG
139 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
140
a0377dfe
FD
141 LTTNG_ASSERT(session);
142 LTTNG_ASSERT(dst);
2f77fc4b
DG
143
144 memset(tmp_urls, 0, sizeof(tmp_urls));
145 memset(tmp_uurl, 0, sizeof(tmp_uurl));
146
147 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
148
149 if (session->kernel_session && session->kernel_session->consumer) {
150 kuri = &session->kernel_session->consumer->dst.net.control;
151 kdata_port = session->kernel_session->consumer->dst.net.data.port;
152 }
153
154 if (session->ust_session && session->ust_session->consumer) {
155 uuri = &session->ust_session->consumer->dst.net.control;
156 udata_port = session->ust_session->consumer->dst.net.data.port;
157 }
158
cd9adb8b 159 if (uuri == nullptr && kuri == nullptr) {
2f77fc4b
DG
160 uri = &session->consumer->dst.net.control;
161 kdata_port = session->consumer->dst.net.data.port;
162 } else if (kuri && uuri) {
163 ret = uri_compare(kuri, uuri);
164 if (ret) {
165 /* Not Equal */
166 uri = kuri;
167 /* Build uuri URL string */
168 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
169 if (ret < 0) {
170 goto error;
171 }
172 } else {
173 uri = kuri;
174 }
cd9adb8b 175 } else if (kuri && uuri == nullptr) {
2f77fc4b 176 uri = kuri;
cd9adb8b 177 } else if (uuri && kuri == nullptr) {
2f77fc4b
DG
178 uri = uuri;
179 }
180
181 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
182 if (ret < 0) {
183 goto error;
184 }
185
9aa9f900
DG
186 /*
187 * Do we have a UST url set. If yes, this means we have both kernel and UST
188 * to print.
189 */
9d035200 190 if (*tmp_uurl != '\0') {
28ab034a
JG
191 ret = snprintf(dst,
192 size,
193 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
194 tmp_urls,
195 kdata_port,
196 tmp_uurl,
197 udata_port);
2f77fc4b 198 } else {
9aa9f900 199 int dport;
bef08707 200 if (kuri || (!kuri && !uuri)) {
9aa9f900
DG
201 dport = kdata_port;
202 } else {
203 /* No kernel URI, use the UST port. */
204 dport = udata_port;
205 }
206 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
2f77fc4b
DG
207 }
208
209error:
210 return ret;
211}
212
fb83fe64
JD
213/*
214 * Get run-time attributes if the session has been started (discarded events,
215 * lost packets).
216 */
217static int get_kernel_runtime_stats(struct ltt_session *session,
28ab034a
JG
218 struct ltt_kernel_channel *kchan,
219 uint64_t *discarded_events,
220 uint64_t *lost_packets)
fb83fe64
JD
221{
222 int ret;
223
224 if (!session->has_been_started) {
225 ret = 0;
226 *discarded_events = 0;
227 *lost_packets = 0;
228 goto end;
229 }
230
28ab034a
JG
231 ret = consumer_get_discarded_events(
232 session->id, kchan->key, session->kernel_session->consumer, discarded_events);
fb83fe64
JD
233 if (ret < 0) {
234 goto end;
235 }
236
28ab034a
JG
237 ret = consumer_get_lost_packets(
238 session->id, kchan->key, session->kernel_session->consumer, lost_packets);
fb83fe64
JD
239 if (ret < 0) {
240 goto end;
241 }
242
243end:
244 return ret;
245}
246
247/*
248 * Get run-time attributes if the session has been started (discarded events,
249 * lost packets).
250 */
251static int get_ust_runtime_stats(struct ltt_session *session,
28ab034a
JG
252 struct ltt_ust_channel *uchan,
253 uint64_t *discarded_events,
254 uint64_t *lost_packets)
fb83fe64
JD
255{
256 int ret;
257 struct ltt_ust_session *usess;
258
a91c5803
JG
259 if (!discarded_events || !lost_packets) {
260 ret = -1;
261 goto end;
262 }
263
fb83fe64 264 usess = session->ust_session;
a0377dfe
FD
265 LTTNG_ASSERT(discarded_events);
266 LTTNG_ASSERT(lost_packets);
fb83fe64
JD
267
268 if (!usess || !session->has_been_started) {
269 *discarded_events = 0;
270 *lost_packets = 0;
271 ret = 0;
272 goto end;
273 }
274
275 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
276 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
28ab034a
JG
277 &usess->buffer_reg_uid_list,
278 usess->consumer,
279 uchan->id,
280 uchan->attr.overwrite,
281 discarded_events,
282 lost_packets);
fb83fe64
JD
283 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
284 ret = ust_app_pid_get_channel_runtime_stats(usess,
28ab034a
JG
285 uchan,
286 usess->consumer,
287 uchan->attr.overwrite,
288 discarded_events,
289 lost_packets);
fb83fe64
JD
290 if (ret < 0) {
291 goto end;
292 }
293 *discarded_events += uchan->per_pid_closed_app_discarded;
294 *lost_packets += uchan->per_pid_closed_app_lost;
295 } else {
296 ERR("Unsupported buffer type");
a0377dfe 297 abort();
fb83fe64
JD
298 ret = -1;
299 goto end;
300 }
301
302end:
303 return ret;
304}
305
3c6a091f 306/*
022d91ba 307 * Create a list of agent domain events.
3c6a091f
DG
308 *
309 * Return number of events in list on success or else a negative value.
310 */
28ab034a
JG
311static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
312 struct lttng_payload *reply_payload,
313 unsigned int *nb_events)
3c6a091f 314{
8ddd72ef
JR
315 enum lttng_error_code ret_code;
316 int ret = 0;
317 unsigned int local_nb_events = 0;
318 struct agent_event *event;
3c6a091f 319 struct lttng_ht_iter iter;
8ddd72ef 320 unsigned long agent_event_count;
3c6a091f 321
8ddd72ef
JR
322 assert(agt);
323 assert(reply_payload);
3c6a091f 324
022d91ba 325 DBG3("Listing agent events");
3c6a091f 326
8ddd72ef
JR
327 agent_event_count = lttng_ht_get_count(agt->events);
328 if (agent_event_count == 0) {
329 /* Early exit. */
330 goto end;
331 }
7966af57 332
8ddd72ef
JR
333 if (agent_event_count > UINT_MAX) {
334 ret_code = LTTNG_ERR_OVERFLOW;
335 goto error;
336 }
e368fb43 337
8ddd72ef 338 local_nb_events = (unsigned int) agent_event_count;
e368fb43 339
56047f5a
JG
340 {
341 lttng::urcu::read_lock_guard read_lock;
8ddd72ef 342
56047f5a
JG
343 cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) {
344 struct lttng_event *tmp_event = lttng_event_create();
b4e3ceb9 345
56047f5a
JG
346 if (!tmp_event) {
347 ret_code = LTTNG_ERR_NOMEM;
348 goto error;
349 }
28ab034a 350
56047f5a
JG
351 if (lttng_strncpy(tmp_event->name, event->name, sizeof(tmp_event->name))) {
352 lttng_event_destroy(tmp_event);
353 ret_code = LTTNG_ERR_FATAL;
354 goto error;
355 }
3c6a091f 356
56047f5a
JG
357 tmp_event->name[sizeof(tmp_event->name) - 1] = '\0';
358 tmp_event->enabled = !!event->enabled_count;
359 tmp_event->loglevel = event->loglevel_value;
360 tmp_event->loglevel_type = event->loglevel_type;
361
362 ret = lttng_event_serialize(tmp_event,
363 0,
364 nullptr,
365 event->filter_expression,
366 0,
367 nullptr,
368 reply_payload);
369 lttng_event_destroy(tmp_event);
370 if (ret) {
371 ret_code = LTTNG_ERR_FATAL;
372 goto error;
373 }
3c02e545 374 }
3c6a091f 375 }
47e52862 376end:
8ddd72ef
JR
377 ret_code = LTTNG_OK;
378 *nb_events = local_nb_events;
379error:
8ddd72ef 380 return ret_code;
3c6a091f
DG
381}
382
2f77fc4b
DG
383/*
384 * Create a list of ust global domain events.
385 */
8ddd72ef 386static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
28ab034a
JG
387 struct ltt_ust_domain_global *ust_global,
388 struct lttng_payload *reply_payload,
389 unsigned int *nb_events)
2f77fc4b 390{
8ddd72ef
JR
391 enum lttng_error_code ret_code;
392 int ret;
2f77fc4b 393 struct lttng_ht_iter iter;
8ddd72ef
JR
394 struct lttng_ht_node_str *node;
395 struct ltt_ust_channel *uchan;
396 struct ltt_ust_event *uevent;
397 unsigned long channel_event_count;
398 unsigned int local_nb_events = 0;
399
400 assert(reply_payload);
401 assert(nb_events);
2f77fc4b
DG
402
403 DBG("Listing UST global events for channel %s", channel_name);
404
56047f5a 405 lttng::urcu::read_lock_guard read_lock;
2f77fc4b 406
e368fb43 407 lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
2f77fc4b 408 node = lttng_ht_iter_get_node_str(&iter);
cd9adb8b 409 if (node == nullptr) {
8ddd72ef 410 ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
e68d8bdb 411 goto error;
2f77fc4b
DG
412 }
413
414 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
415
8ddd72ef
JR
416 channel_event_count = lttng_ht_get_count(uchan->events);
417 if (channel_event_count == 0) {
418 /* Early exit. */
419 ret_code = LTTNG_OK;
420 goto end;
421 }
422
423 if (channel_event_count > UINT_MAX) {
424 ret_code = LTTNG_ERR_OVERFLOW;
425 goto error;
426 }
427
428 local_nb_events = (unsigned int) channel_event_count;
429
430 DBG3("Listing UST global %d events", *nb_events);
2f77fc4b 431
28ab034a 432 cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) {
cd9adb8b 433 struct lttng_event *tmp_event = nullptr;
e368fb43 434
b4e3ceb9 435 if (uevent->internal) {
8ddd72ef
JR
436 /* This event should remain hidden from clients */
437 local_nb_events--;
b4e3ceb9
PP
438 continue;
439 }
440
8ddd72ef
JR
441 tmp_event = lttng_event_create();
442 if (!tmp_event) {
443 ret_code = LTTNG_ERR_NOMEM;
e68d8bdb 444 goto error;
43ed1485 445 }
2f77fc4b 446
28ab034a 447 if (lttng_strncpy(tmp_event->name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN)) {
8ddd72ef
JR
448 ret_code = LTTNG_ERR_FATAL;
449 lttng_event_destroy(tmp_event);
e68d8bdb 450 goto error;
8ddd72ef
JR
451 }
452
453 tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
454 tmp_event->enabled = uevent->enabled;
2f77fc4b
DG
455
456 switch (uevent->attr.instrumentation) {
fc4b93fa 457 case LTTNG_UST_ABI_TRACEPOINT:
8ddd72ef 458 tmp_event->type = LTTNG_EVENT_TRACEPOINT;
2f77fc4b 459 break;
fc4b93fa 460 case LTTNG_UST_ABI_PROBE:
8ddd72ef 461 tmp_event->type = LTTNG_EVENT_PROBE;
2f77fc4b 462 break;
fc4b93fa 463 case LTTNG_UST_ABI_FUNCTION:
8ddd72ef 464 tmp_event->type = LTTNG_EVENT_FUNCTION;
2f77fc4b
DG
465 break;
466 }
467
8ddd72ef 468 tmp_event->loglevel = uevent->attr.loglevel;
2f77fc4b 469 switch (uevent->attr.loglevel_type) {
fc4b93fa 470 case LTTNG_UST_ABI_LOGLEVEL_ALL:
8ddd72ef 471 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2f77fc4b 472 break;
fc4b93fa 473 case LTTNG_UST_ABI_LOGLEVEL_RANGE:
8ddd72ef 474 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
2f77fc4b 475 break;
fc4b93fa 476 case LTTNG_UST_ABI_LOGLEVEL_SINGLE:
8ddd72ef 477 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
2f77fc4b
DG
478 break;
479 }
480 if (uevent->filter) {
8ddd72ef 481 tmp_event->filter = 1;
2f77fc4b 482 }
4634f12e 483 if (uevent->exclusion) {
8ddd72ef 484 tmp_event->exclusion = 1;
4634f12e 485 }
b4e3ceb9 486
dd7ef124
JG
487 std::vector<const char *> exclusion_names;
488 if (uevent->exclusion) {
489 for (int i = 0; i < uevent->exclusion->count; i++) {
490 exclusion_names.emplace_back(
491 LTTNG_EVENT_EXCLUSION_NAME_AT(uevent->exclusion, i));
492 }
493 }
494
8ddd72ef
JR
495 /*
496 * We do not care about the filter bytecode and the fd from the
497 * userspace_probe_location.
498 */
dd7ef124
JG
499 ret = lttng_event_serialize(
500 tmp_event,
501 exclusion_names.size(),
502 exclusion_names.size() ?
503 exclusion_names.data() :
504 nullptr,
505 uevent->filter_expression,
506 0,
507 nullptr,
508 reply_payload);
8ddd72ef 509 lttng_event_destroy(tmp_event);
3c02e545 510 if (ret) {
8ddd72ef
JR
511 ret_code = LTTNG_ERR_FATAL;
512 goto error;
3c02e545 513 }
2f77fc4b
DG
514 }
515
d31d3e8c 516end:
8ddd72ef
JR
517 /* nb_events is already set at this point. */
518 ret_code = LTTNG_OK;
519 *nb_events = local_nb_events;
520error:
8ddd72ef 521 return ret_code;
2f77fc4b
DG
522}
523
524/*
525 * Fill lttng_event array of all kernel events in the channel.
526 */
8ddd72ef 527static enum lttng_error_code list_lttng_kernel_events(char *channel_name,
28ab034a
JG
528 struct ltt_kernel_session *kernel_session,
529 struct lttng_payload *reply_payload,
530 unsigned int *nb_events)
2f77fc4b 531{
8ddd72ef 532 enum lttng_error_code ret_code;
e368fb43 533 int ret;
8ddd72ef
JR
534 struct ltt_kernel_event *event;
535 struct ltt_kernel_channel *kchan;
536
537 assert(reply_payload);
2f77fc4b
DG
538
539 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
cd9adb8b 540 if (kchan == nullptr) {
8ddd72ef
JR
541 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
542 goto end;
2f77fc4b
DG
543 }
544
8ddd72ef 545 *nb_events = kchan->event_count;
2f77fc4b
DG
546
547 DBG("Listing events for channel %s", kchan->channel->name);
548
8ddd72ef
JR
549 if (*nb_events == 0) {
550 ret_code = LTTNG_OK;
551 goto end;
552 }
553
e368fb43 554 /* Kernel channels */
28ab034a 555 cds_list_for_each_entry (event, &kchan->events_list.head, list) {
8ddd72ef 556 struct lttng_event *tmp_event = lttng_event_create();
2f77fc4b 557
8ddd72ef
JR
558 if (!tmp_event) {
559 ret_code = LTTNG_ERR_NOMEM;
560 goto end;
561 }
562
563 if (lttng_strncpy(tmp_event->name, event->event->name, LTTNG_SYMBOL_NAME_LEN)) {
564 lttng_event_destroy(tmp_event);
565 ret_code = LTTNG_ERR_FATAL;
43ed1485
JG
566 goto end;
567 }
568
8ddd72ef
JR
569 tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
570 tmp_event->enabled = event->enabled;
571 tmp_event->filter = (unsigned char) !!event->filter_expression;
b4e3ceb9 572
8ddd72ef 573 switch (event->event->instrumentation) {
b8e2fb80 574 case LTTNG_KERNEL_ABI_TRACEPOINT:
8ddd72ef 575 tmp_event->type = LTTNG_EVENT_TRACEPOINT;
2f77fc4b 576 break;
b8e2fb80 577 case LTTNG_KERNEL_ABI_KRETPROBE:
8ddd72ef 578 tmp_event->type = LTTNG_EVENT_FUNCTION;
28ab034a
JG
579 memcpy(&tmp_event->attr.probe,
580 &event->event->u.kprobe,
581 sizeof(struct lttng_kernel_abi_kprobe));
1896972b 582 break;
b8e2fb80 583 case LTTNG_KERNEL_ABI_KPROBE:
8ddd72ef 584 tmp_event->type = LTTNG_EVENT_PROBE;
28ab034a
JG
585 memcpy(&tmp_event->attr.probe,
586 &event->event->u.kprobe,
587 sizeof(struct lttng_kernel_abi_kprobe));
2f77fc4b 588 break;
b8e2fb80 589 case LTTNG_KERNEL_ABI_UPROBE:
8ddd72ef 590 tmp_event->type = LTTNG_EVENT_USERSPACE_PROBE;
b955b4d4 591 break;
b8e2fb80 592 case LTTNG_KERNEL_ABI_FUNCTION:
8ddd72ef 593 tmp_event->type = LTTNG_EVENT_FUNCTION;
28ab034a
JG
594 memcpy(&(tmp_event->attr.ftrace),
595 &event->event->u.ftrace,
596 sizeof(struct lttng_kernel_abi_function));
2f77fc4b 597 break;
b8e2fb80 598 case LTTNG_KERNEL_ABI_NOOP:
8ddd72ef 599 tmp_event->type = LTTNG_EVENT_NOOP;
2f77fc4b 600 break;
b8e2fb80 601 case LTTNG_KERNEL_ABI_SYSCALL:
8ddd72ef 602 tmp_event->type = LTTNG_EVENT_SYSCALL;
2f77fc4b 603 break;
b8e2fb80 604 case LTTNG_KERNEL_ABI_ALL:
1ab8c2ad
FD
605 /* fall-through. */
606 default:
a0377dfe 607 abort();
2f77fc4b
DG
608 break;
609 }
b4e3ceb9 610
8ddd72ef
JR
611 if (event->userspace_probe_location) {
612 struct lttng_userspace_probe_location *location_copy =
28ab034a
JG
613 lttng_userspace_probe_location_copy(
614 event->userspace_probe_location);
8ddd72ef
JR
615
616 if (!location_copy) {
617 lttng_event_destroy(tmp_event);
618 ret_code = LTTNG_ERR_NOMEM;
619 goto end;
620 }
621
28ab034a 622 ret = lttng_event_set_userspace_probe_location(tmp_event, location_copy);
8ddd72ef
JR
623 if (ret) {
624 lttng_event_destroy(tmp_event);
28ab034a 625 lttng_userspace_probe_location_destroy(location_copy);
8ddd72ef
JR
626 ret_code = LTTNG_ERR_INVALID;
627 goto end;
628 }
e368fb43 629 }
e368fb43 630
28ab034a 631 ret = lttng_event_serialize(
cd9adb8b 632 tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload);
8ddd72ef 633 lttng_event_destroy(tmp_event);
3c02e545 634 if (ret) {
8ddd72ef
JR
635 ret_code = LTTNG_ERR_FATAL;
636 goto end;
3c02e545 637 }
2f77fc4b
DG
638 }
639
8ddd72ef 640 ret_code = LTTNG_OK;
db906c12 641end:
8ddd72ef 642 return ret_code;
2f77fc4b
DG
643}
644
645/*
646 * Add URI so the consumer output object. Set the correct path depending on the
647 * domain adding the default trace directory.
648 */
28ab034a
JG
649static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *session,
650 struct consumer_output *consumer,
651 struct lttng_uri *uri,
652 enum lttng_domain_type domain)
2f77fc4b 653{
b178f53e
JG
654 int ret;
655 enum lttng_error_code ret_code = LTTNG_OK;
2f77fc4b 656
a0377dfe 657 LTTNG_ASSERT(uri);
2f77fc4b 658
cd9adb8b 659 if (consumer == nullptr) {
2f77fc4b 660 DBG("No consumer detected. Don't add URI. Stopping.");
b178f53e 661 ret_code = LTTNG_ERR_NO_CONSUMER;
2f77fc4b
DG
662 goto error;
663 }
664
665 switch (domain) {
666 case LTTNG_DOMAIN_KERNEL:
b178f53e 667 ret = lttng_strncpy(consumer->domain_subdir,
28ab034a
JG
668 DEFAULT_KERNEL_TRACE_DIR,
669 sizeof(consumer->domain_subdir));
2f77fc4b
DG
670 break;
671 case LTTNG_DOMAIN_UST:
b178f53e 672 ret = lttng_strncpy(consumer->domain_subdir,
28ab034a
JG
673 DEFAULT_UST_TRACE_DIR,
674 sizeof(consumer->domain_subdir));
2f77fc4b
DG
675 break;
676 default:
677 /*
b178f53e
JG
678 * This case is possible is we try to add the URI to the global
679 * tracing session consumer object which in this case there is
680 * no subdir.
2f77fc4b 681 */
28ab034a 682 memset(consumer->domain_subdir, 0, sizeof(consumer->domain_subdir));
b178f53e
JG
683 ret = 0;
684 }
685 if (ret) {
686 ERR("Failed to initialize consumer output domain subdirectory");
687 ret_code = LTTNG_ERR_FATAL;
688 goto error;
2f77fc4b
DG
689 }
690
691 switch (uri->dtype) {
692 case LTTNG_DST_IPV4:
693 case LTTNG_DST_IPV6:
694 DBG2("Setting network URI to consumer");
695
df75acac
DG
696 if (consumer->type == CONSUMER_DST_NET) {
697 if ((uri->stype == LTTNG_STREAM_CONTROL &&
28ab034a
JG
698 consumer->dst.net.control_isset) ||
699 (uri->stype == LTTNG_STREAM_DATA && consumer->dst.net.data_isset)) {
b178f53e 700 ret_code = LTTNG_ERR_URL_EXIST;
df75acac
DG
701 goto error;
702 }
703 } else {
b178f53e 704 memset(&consumer->dst, 0, sizeof(consumer->dst));
785d2d0d
DG
705 }
706
2f77fc4b 707 /* Set URI into consumer output object */
b178f53e 708 ret = consumer_set_network_uri(session, consumer, uri);
2f77fc4b 709 if (ret < 0) {
7966af57 710 ret_code = (lttng_error_code) -ret;
2f77fc4b
DG
711 goto error;
712 } else if (ret == 1) {
713 /*
714 * URI was the same in the consumer so we do not append the subdir
715 * again so to not duplicate output dir.
716 */
b178f53e 717 ret_code = LTTNG_OK;
2f77fc4b
DG
718 goto error;
719 }
2f77fc4b
DG
720 break;
721 case LTTNG_DST_PATH:
b178f53e
JG
722 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
723 ret_code = LTTNG_ERR_INVALID;
9ac05d92
MD
724 goto error;
725 }
28ab034a 726 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
b178f53e
JG
727 memset(&consumer->dst, 0, sizeof(consumer->dst));
728
729 ret = lttng_strncpy(consumer->dst.session_root_path,
28ab034a
JG
730 uri->dst.path,
731 sizeof(consumer->dst.session_root_path));
4df41cad
JG
732 if (ret) {
733 ret_code = LTTNG_ERR_FATAL;
734 goto error;
735 }
2f77fc4b
DG
736 consumer->type = CONSUMER_DST_LOCAL;
737 break;
738 }
739
b178f53e 740 ret_code = LTTNG_OK;
2f77fc4b 741error:
b178f53e 742 return ret_code;
2f77fc4b
DG
743}
744
745/*
746 * Init tracing by creating trace directory and sending fds kernel consumer.
747 */
748static int init_kernel_tracing(struct ltt_kernel_session *session)
749{
750 int ret = 0;
751 struct lttng_ht_iter iter;
752 struct consumer_socket *socket;
753
a0377dfe 754 LTTNG_ASSERT(session);
2f77fc4b 755
cd9adb8b 756 if (session->consumer_fds_sent == 0 && session->consumer != nullptr) {
56047f5a
JG
757 lttng::urcu::read_lock_guard read_lock;
758
28ab034a
JG
759 cds_lfht_for_each_entry (
760 session->consumer->socks->ht, &iter.iter, socket, node.node) {
2f77fc4b 761 pthread_mutex_lock(socket->lock);
f50f23d9 762 ret = kernel_consumer_send_session(socket, session);
2f77fc4b
DG
763 pthread_mutex_unlock(socket->lock);
764 if (ret < 0) {
f73fabfd 765 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
766 goto error;
767 }
768 }
769 }
770
771error:
772 return ret;
773}
774
775/*
776 * Create a socket to the relayd using the URI.
777 *
778 * On success, the relayd_sock pointer is set to the created socket.
9a654598 779 * Else, it remains untouched and an LTTng error code is returned.
2f77fc4b 780 */
9a654598 781static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
28ab034a
JG
782 struct lttcomm_relayd_sock **relayd_sock,
783 struct consumer_output *consumer)
2f77fc4b
DG
784{
785 int ret;
9a654598 786 enum lttng_error_code status = LTTNG_OK;
6151a90f 787 struct lttcomm_relayd_sock *rsock;
2f77fc4b 788
28ab034a
JG
789 rsock = lttcomm_alloc_relayd_sock(
790 uri, RELAYD_VERSION_COMM_MAJOR, RELAYD_VERSION_COMM_MINOR);
6151a90f 791 if (!rsock) {
9a654598 792 status = LTTNG_ERR_FATAL;
2f77fc4b
DG
793 goto error;
794 }
795
ffe60014
DG
796 /*
797 * Connect to relayd so we can proceed with a session creation. This call
798 * can possibly block for an arbitrary amount of time to set the health
799 * state to be in poll execution.
800 */
801 health_poll_entry();
6151a90f 802 ret = relayd_connect(rsock);
ffe60014 803 health_poll_exit();
2f77fc4b
DG
804 if (ret < 0) {
805 ERR("Unable to reach lttng-relayd");
9a654598 806 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
807 goto free_sock;
808 }
809
810 /* Create socket for control stream. */
811 if (uri->stype == LTTNG_STREAM_CONTROL) {
eacb7b6f
MD
812 uint64_t result_flags;
813
2f77fc4b
DG
814 DBG3("Creating relayd stream socket from URI");
815
816 /* Check relayd version */
6151a90f 817 ret = relayd_version_check(rsock);
67d5aa28 818 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
9a654598 819 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
67d5aa28
JD
820 goto close_sock;
821 } else if (ret < 0) {
822 ERR("Unable to reach lttng-relayd");
9a654598 823 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
824 goto close_sock;
825 }
b31610f2
JD
826 consumer->relay_major_version = rsock->major;
827 consumer->relay_minor_version = rsock->minor;
28ab034a 828 ret = relayd_get_configuration(rsock, 0, &result_flags);
eacb7b6f
MD
829 if (ret < 0) {
830 ERR("Unable to get relayd configuration");
831 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
832 goto close_sock;
833 }
834 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
835 consumer->relay_allows_clear = true;
836 }
2f77fc4b
DG
837 } else if (uri->stype == LTTNG_STREAM_DATA) {
838 DBG3("Creating relayd data socket from URI");
839 } else {
840 /* Command is not valid */
841 ERR("Relayd invalid stream type: %d", uri->stype);
9a654598 842 status = LTTNG_ERR_INVALID;
2f77fc4b
DG
843 goto close_sock;
844 }
845
6151a90f 846 *relayd_sock = rsock;
2f77fc4b 847
9a654598 848 return status;
2f77fc4b
DG
849
850close_sock:
6151a90f
JD
851 /* The returned value is not useful since we are on an error path. */
852 (void) relayd_close(rsock);
2f77fc4b 853free_sock:
6151a90f 854 free(rsock);
2f77fc4b 855error:
9a654598 856 return status;
2f77fc4b
DG
857}
858
859/*
860 * Connect to the relayd using URI and send the socket to the right consumer.
43fade62
JG
861 *
862 * The consumer socket lock must be held by the caller.
9a654598
JG
863 *
864 * Returns LTTNG_OK on success or an LTTng error code on failure.
2f77fc4b 865 */
28ab034a
JG
866static enum lttng_error_code send_consumer_relayd_socket(unsigned int session_id,
867 struct lttng_uri *relayd_uri,
868 struct consumer_output *consumer,
869 struct consumer_socket *consumer_sock,
870 const char *session_name,
871 const char *hostname,
872 const char *base_path,
873 int session_live_timer,
874 const uint64_t *current_chunk_id,
875 time_t session_creation_time,
876 bool session_name_contains_creation_time)
2f77fc4b
DG
877{
878 int ret;
cd9adb8b 879 struct lttcomm_relayd_sock *rsock = nullptr;
9a654598 880 enum lttng_error_code status;
2f77fc4b 881
ffe60014 882 /* Connect to relayd and make version check if uri is the control. */
9a654598
JG
883 status = create_connect_relayd(relayd_uri, &rsock, consumer);
884 if (status != LTTNG_OK) {
9e218353 885 goto relayd_comm_error;
ffe60014 886 }
a0377dfe 887 LTTNG_ASSERT(rsock);
ffe60014 888
2f77fc4b 889 /* Set the network sequence index if not set. */
d88aee68
DG
890 if (consumer->net_seq_index == (uint64_t) -1ULL) {
891 pthread_mutex_lock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
892 /*
893 * Increment net_seq_idx because we are about to transfer the
894 * new relayd socket to the consumer.
d88aee68 895 * Assign unique key so the consumer can match streams.
2f77fc4b 896 */
d88aee68
DG
897 consumer->net_seq_index = ++relayd_net_seq_idx;
898 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
899 }
900
2f77fc4b 901 /* Send relayd socket to consumer. */
28ab034a
JG
902 ret = consumer_send_relayd_socket(consumer_sock,
903 rsock,
904 consumer,
905 relayd_uri->stype,
906 session_id,
907 session_name,
908 hostname,
909 base_path,
910 session_live_timer,
911 current_chunk_id,
912 session_creation_time,
913 session_name_contains_creation_time);
2f77fc4b 914 if (ret < 0) {
9a654598 915 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2f77fc4b
DG
916 goto close_sock;
917 }
918
c890b720
DG
919 /* Flag that the corresponding socket was sent. */
920 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
ffe60014 921 consumer_sock->control_sock_sent = 1;
c890b720 922 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
ffe60014 923 consumer_sock->data_sock_sent = 1;
c890b720
DG
924 }
925
2f77fc4b
DG
926 /*
927 * Close socket which was dup on the consumer side. The session daemon does
928 * NOT keep track of the relayd socket(s) once transfer to the consumer.
929 */
930
931close_sock:
9a654598 932 if (status != LTTNG_OK) {
ffe60014 933 /*
d9078d0c
DG
934 * The consumer output for this session should not be used anymore
935 * since the relayd connection failed thus making any tracing or/and
936 * streaming not usable.
ffe60014 937 */
66cefebd 938 consumer->enabled = false;
ffe60014 939 }
9e218353
JR
940 (void) relayd_close(rsock);
941 free(rsock);
942
943relayd_comm_error:
9a654598 944 return status;
2f77fc4b
DG
945}
946
947/*
948 * Send both relayd sockets to a specific consumer and domain. This is a
949 * helper function to facilitate sending the information to the consumer for a
950 * session.
43fade62
JG
951 *
952 * The consumer socket lock must be held by the caller.
9a654598
JG
953 *
954 * Returns LTTNG_OK, or an LTTng error code on failure.
2f77fc4b 955 */
28ab034a
JG
956static enum lttng_error_code send_consumer_relayd_sockets(unsigned int session_id,
957 struct consumer_output *consumer,
958 struct consumer_socket *sock,
959 const char *session_name,
960 const char *hostname,
961 const char *base_path,
962 int session_live_timer,
963 const uint64_t *current_chunk_id,
964 time_t session_creation_time,
965 bool session_name_contains_creation_time)
2f77fc4b 966{
9a654598 967 enum lttng_error_code status = LTTNG_OK;
2f77fc4b 968
a0377dfe
FD
969 LTTNG_ASSERT(consumer);
970 LTTNG_ASSERT(sock);
2f77fc4b 971
2f77fc4b 972 /* Sending control relayd socket. */
ffe60014 973 if (!sock->control_sock_sent) {
9a654598 974 status = send_consumer_relayd_socket(session_id,
28ab034a
JG
975 &consumer->dst.net.control,
976 consumer,
977 sock,
978 session_name,
979 hostname,
980 base_path,
981 session_live_timer,
982 current_chunk_id,
983 session_creation_time,
984 session_name_contains_creation_time);
9a654598 985 if (status != LTTNG_OK) {
c890b720
DG
986 goto error;
987 }
2f77fc4b
DG
988 }
989
990 /* Sending data relayd socket. */
ffe60014 991 if (!sock->data_sock_sent) {
9a654598 992 status = send_consumer_relayd_socket(session_id,
28ab034a
JG
993 &consumer->dst.net.data,
994 consumer,
995 sock,
996 session_name,
997 hostname,
998 base_path,
999 session_live_timer,
1000 current_chunk_id,
1001 session_creation_time,
1002 session_name_contains_creation_time);
9a654598 1003 if (status != LTTNG_OK) {
c890b720
DG
1004 goto error;
1005 }
2f77fc4b
DG
1006 }
1007
2f77fc4b 1008error:
9a654598 1009 return status;
2f77fc4b
DG
1010}
1011
1012/*
1013 * Setup relayd connections for a tracing session. First creates the socket to
1014 * the relayd and send them to the right domain consumer. Consumer type MUST be
1015 * network.
1016 */
ffe60014 1017int cmd_setup_relayd(struct ltt_session *session)
2f77fc4b 1018{
f73fabfd 1019 int ret = LTTNG_OK;
2f77fc4b
DG
1020 struct ltt_ust_session *usess;
1021 struct ltt_kernel_session *ksess;
1022 struct consumer_socket *socket;
1023 struct lttng_ht_iter iter;
0e270a1e 1024 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
2f77fc4b 1025
a0377dfe 1026 LTTNG_ASSERT(session);
2f77fc4b
DG
1027
1028 usess = session->ust_session;
1029 ksess = session->kernel_session;
1030
785d2d0d 1031 DBG("Setting relayd for session %s", session->name);
2f77fc4b 1032
1e791a74
JG
1033 if (session->current_trace_chunk) {
1034 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
28ab034a 1035 session->current_trace_chunk, &current_chunk_id.value);
1e791a74
JG
1036
1037 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1038 current_chunk_id.is_set = true;
1039 } else {
1040 ERR("Failed to get current trace chunk id");
1041 ret = LTTNG_ERR_UNK;
1042 goto error;
1043 }
1044 }
1045
28ab034a
JG
1046 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET &&
1047 usess->consumer->enabled) {
2f77fc4b 1048 /* For each consumer socket, send relayd sockets */
56047f5a
JG
1049 lttng::urcu::read_lock_guard read_lock;
1050
28ab034a
JG
1051 cds_lfht_for_each_entry (
1052 usess->consumer->socks->ht, &iter.iter, socket, node.node) {
2f77fc4b 1053 pthread_mutex_lock(socket->lock);
28ab034a
JG
1054 ret = send_consumer_relayd_sockets(
1055 session->id,
1056 usess->consumer,
1057 socket,
1058 session->name,
1059 session->hostname,
1060 session->base_path,
1061 session->live_timer,
cd9adb8b 1062 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
28ab034a
JG
1063 session->creation_time,
1064 session->name_contains_creation_time);
2f77fc4b 1065 pthread_mutex_unlock(socket->lock);
f73fabfd 1066 if (ret != LTTNG_OK) {
2f77fc4b
DG
1067 goto error;
1068 }
6dc3064a
DG
1069 /* Session is now ready for network streaming. */
1070 session->net_handle = 1;
2f77fc4b 1071 }
56047f5a 1072
28ab034a
JG
1073 session->consumer->relay_major_version = usess->consumer->relay_major_version;
1074 session->consumer->relay_minor_version = usess->consumer->relay_minor_version;
1075 session->consumer->relay_allows_clear = usess->consumer->relay_allows_clear;
2f77fc4b
DG
1076 }
1077
28ab034a
JG
1078 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET &&
1079 ksess->consumer->enabled) {
56047f5a
JG
1080 lttng::urcu::read_lock_guard read_lock;
1081
28ab034a
JG
1082 cds_lfht_for_each_entry (
1083 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
2f77fc4b 1084 pthread_mutex_lock(socket->lock);
28ab034a
JG
1085 ret = send_consumer_relayd_sockets(
1086 session->id,
1087 ksess->consumer,
1088 socket,
1089 session->name,
1090 session->hostname,
1091 session->base_path,
1092 session->live_timer,
cd9adb8b 1093 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
28ab034a
JG
1094 session->creation_time,
1095 session->name_contains_creation_time);
2f77fc4b 1096 pthread_mutex_unlock(socket->lock);
f73fabfd 1097 if (ret != LTTNG_OK) {
2f77fc4b
DG
1098 goto error;
1099 }
6dc3064a
DG
1100 /* Session is now ready for network streaming. */
1101 session->net_handle = 1;
2f77fc4b 1102 }
56047f5a 1103
28ab034a
JG
1104 session->consumer->relay_major_version = ksess->consumer->relay_major_version;
1105 session->consumer->relay_minor_version = ksess->consumer->relay_minor_version;
1106 session->consumer->relay_allows_clear = ksess->consumer->relay_allows_clear;
2f77fc4b
DG
1107 }
1108
1109error:
1110 return ret;
1111}
1112
9b6c7ec5
DG
1113/*
1114 * Start a kernel session by opening all necessary streams.
1115 */
4dbe1875 1116int start_kernel_session(struct ltt_kernel_session *ksess)
9b6c7ec5
DG
1117{
1118 int ret;
1119 struct ltt_kernel_channel *kchan;
1120
1121 /* Open kernel metadata */
cd9adb8b 1122 if (ksess->metadata == nullptr && ksess->output_traces) {
9b6c7ec5
DG
1123 ret = kernel_open_metadata(ksess);
1124 if (ret < 0) {
1125 ret = LTTNG_ERR_KERN_META_FAIL;
1126 goto error;
1127 }
1128 }
1129
1130 /* Open kernel metadata stream */
07b86b52 1131 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
9b6c7ec5
DG
1132 ret = kernel_open_metadata_stream(ksess);
1133 if (ret < 0) {
1134 ERR("Kernel create metadata stream failed");
1135 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1136 goto error;
1137 }
1138 }
1139
1140 /* For each channel */
28ab034a 1141 cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
9b6c7ec5
DG
1142 if (kchan->stream_count == 0) {
1143 ret = kernel_open_channel_stream(kchan);
1144 if (ret < 0) {
1145 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1146 goto error;
1147 }
1148 /* Update the stream global counter */
1149 ksess->stream_count_global += ret;
1150 }
1151 }
1152
1153 /* Setup kernel consumer socket and send fds to it */
1154 ret = init_kernel_tracing(ksess);
e43c41c5 1155 if (ret != 0) {
9b6c7ec5
DG
1156 ret = LTTNG_ERR_KERN_START_FAIL;
1157 goto error;
1158 }
1159
1160 /* This start the kernel tracing */
1161 ret = kernel_start_session(ksess);
1162 if (ret < 0) {
1163 ret = LTTNG_ERR_KERN_START_FAIL;
1164 goto error;
1165 }
1166
1167 /* Quiescent wait after starting trace */
7d268848 1168 kernel_wait_quiescent();
9b6c7ec5 1169
66cefebd 1170 ksess->active = true;
9b6c7ec5
DG
1171
1172 ret = LTTNG_OK;
1173
1174error:
1175 return ret;
1176}
1177
4dbe1875
MD
1178int stop_kernel_session(struct ltt_kernel_session *ksess)
1179{
1180 struct ltt_kernel_channel *kchan;
1181 bool error_occurred = false;
1182 int ret;
1183
1184 if (!ksess || !ksess->active) {
1185 return LTTNG_OK;
1186 }
1187 DBG("Stopping kernel tracing");
1188
1189 ret = kernel_stop_session(ksess);
1190 if (ret < 0) {
1191 ret = LTTNG_ERR_KERN_STOP_FAIL;
1192 goto error;
1193 }
1194
1195 kernel_wait_quiescent();
1196
1197 /* Flush metadata after stopping (if exists) */
1198 if (ksess->metadata_stream_fd >= 0) {
1199 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1200 if (ret < 0) {
1201 ERR("Kernel metadata flush failed");
1202 error_occurred = true;
1203 }
1204 }
1205
1206 /* Flush all buffers after stopping */
28ab034a 1207 cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
4dbe1875
MD
1208 ret = kernel_flush_buffer(kchan);
1209 if (ret < 0) {
1210 ERR("Kernel flush buffer error");
1211 error_occurred = true;
1212 }
1213 }
1214
66cefebd 1215 ksess->active = false;
4dbe1875
MD
1216 if (error_occurred) {
1217 ret = LTTNG_ERR_UNK;
1218 } else {
1219 ret = LTTNG_OK;
1220 }
1221error:
1222 return ret;
1223}
1224
2f77fc4b
DG
1225/*
1226 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1227 */
56a37563 1228int cmd_disable_channel(struct ltt_session *session,
28ab034a
JG
1229 enum lttng_domain_type domain,
1230 char *channel_name)
2f77fc4b
DG
1231{
1232 int ret;
1233 struct ltt_ust_session *usess;
1234
1235 usess = session->ust_session;
1236
56047f5a 1237 lttng::urcu::read_lock_guard read_lock;
2223c96f 1238
2f77fc4b
DG
1239 switch (domain) {
1240 case LTTNG_DOMAIN_KERNEL:
1241 {
28ab034a 1242 ret = channel_kernel_disable(session->kernel_session, channel_name);
f73fabfd 1243 if (ret != LTTNG_OK) {
2f77fc4b
DG
1244 goto error;
1245 }
1246
7d268848 1247 kernel_wait_quiescent();
2f77fc4b
DG
1248 break;
1249 }
1250 case LTTNG_DOMAIN_UST:
1251 {
1252 struct ltt_ust_channel *uchan;
1253 struct lttng_ht *chan_ht;
1254
1255 chan_ht = usess->domain_global.channels;
1256
1257 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
cd9adb8b 1258 if (uchan == nullptr) {
f73fabfd 1259 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1260 goto error;
1261 }
1262
7972aab2 1263 ret = channel_ust_disable(usess, uchan);
f73fabfd 1264 if (ret != LTTNG_OK) {
2f77fc4b
DG
1265 goto error;
1266 }
1267 break;
1268 }
2f77fc4b 1269 default:
f73fabfd 1270 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1271 goto error;
1272 }
1273
f73fabfd 1274 ret = LTTNG_OK;
2f77fc4b
DG
1275
1276error:
1277 return ret;
1278}
1279
1280/*
1281 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1282 *
1283 * The wpipe arguments is used as a notifier for the kernel thread.
1284 */
999af9c1
JR
1285int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe)
1286{
1287 int ret;
1288 size_t channel_len;
1289 ssize_t sock_recv_len;
cd9adb8b 1290 struct lttng_channel *channel = nullptr;
999af9c1
JR
1291 struct lttng_buffer_view view;
1292 struct lttng_dynamic_buffer channel_buffer;
1293 const struct lttng_domain command_domain = cmd_ctx->lsm.domain;
1294
1295 lttng_dynamic_buffer_init(&channel_buffer);
1296 channel_len = (size_t) cmd_ctx->lsm.u.channel.length;
1297 ret = lttng_dynamic_buffer_set_size(&channel_buffer, channel_len);
1298 if (ret) {
1299 ret = LTTNG_ERR_NOMEM;
1300 goto end;
1301 }
1302
28ab034a 1303 sock_recv_len = lttcomm_recv_unix_sock(sock, channel_buffer.data, channel_len);
999af9c1
JR
1304 if (sock_recv_len < 0 || sock_recv_len != channel_len) {
1305 ERR("Failed to receive \"enable channel\" command payload");
1306 ret = LTTNG_ERR_INVALID;
1307 goto end;
1308 }
1309
1310 view = lttng_buffer_view_from_dynamic_buffer(&channel_buffer, 0, channel_len);
1311 if (!lttng_buffer_view_is_valid(&view)) {
1312 ret = LTTNG_ERR_INVALID;
1313 goto end;
1314 }
1315
1316 if (lttng_channel_create_from_buffer(&view, &channel) != channel_len) {
1317 ERR("Invalid channel payload received in \"enable channel\" command");
1318 ret = LTTNG_ERR_INVALID;
1319 goto end;
1320 }
1321
28ab034a 1322 ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe);
999af9c1
JR
1323
1324end:
1325 lttng_dynamic_buffer_reset(&channel_buffer);
1326 lttng_channel_destroy(channel);
1327 return ret;
1328}
1329
28ab034a
JG
1330static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
1331 const struct lttng_domain *domain,
1332 const struct lttng_channel *_attr,
1333 int wpipe)
2f77fc4b 1334{
4878de5c 1335 enum lttng_error_code ret_code;
2f77fc4b
DG
1336 struct ltt_ust_session *usess = session->ust_session;
1337 struct lttng_ht *chan_ht;
1f345e94 1338 size_t len;
cd9adb8b 1339 struct lttng_channel *attr = nullptr;
2f77fc4b 1340
a0377dfe
FD
1341 LTTNG_ASSERT(session);
1342 LTTNG_ASSERT(_attr);
1343 LTTNG_ASSERT(domain);
2f77fc4b 1344
56047f5a
JG
1345 lttng::urcu::read_lock_guard read_lock;
1346
999af9c1
JR
1347 attr = lttng_channel_copy(_attr);
1348 if (!attr) {
4878de5c 1349 ret_code = LTTNG_ERR_NOMEM;
999af9c1
JR
1350 goto end;
1351 }
1352
1353 len = lttng_strnlen(attr->name, sizeof(attr->name));
1f345e94
PP
1354
1355 /* Validate channel name */
cd9adb8b 1356 if (attr->name[0] == '.' || memchr(attr->name, '/', len) != nullptr) {
4878de5c 1357 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1f345e94
PP
1358 goto end;
1359 }
1360
999af9c1 1361 DBG("Enabling channel %s for session %s", attr->name, session->name);
2f77fc4b 1362
ecc48a90
JD
1363 /*
1364 * If the session is a live session, remove the switch timer, the
1365 * live timer does the same thing but sends also synchronisation
1366 * beacons for inactive streams.
1367 */
1368 if (session->live_timer > 0) {
999af9c1
JR
1369 attr->attr.live_timer_interval = session->live_timer;
1370 attr->attr.switch_timer_interval = 0;
ecc48a90
JD
1371 }
1372
6e21424e
JR
1373 /* Check for feature support */
1374 switch (domain->type) {
1375 case LTTNG_DOMAIN_KERNEL:
1376 {
7d268848 1377 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
6e21424e
JR
1378 /* Sampling position of buffer is not supported */
1379 WARN("Kernel tracer does not support buffer monitoring. "
28ab034a
JG
1380 "Setting the monitor interval timer to 0 "
1381 "(disabled) for channel '%s' of session '%s'",
1382 attr->name,
1383 session->name);
999af9c1 1384 lttng_channel_set_monitor_timer_interval(attr, 0);
6e21424e
JR
1385 }
1386 break;
1387 }
1388 case LTTNG_DOMAIN_UST:
f28f9e44 1389 break;
6e21424e
JR
1390 case LTTNG_DOMAIN_JUL:
1391 case LTTNG_DOMAIN_LOG4J:
1392 case LTTNG_DOMAIN_PYTHON:
f28f9e44
JG
1393 if (!agent_tracing_is_enabled()) {
1394 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
4878de5c 1395 ret_code = LTTNG_ERR_AGENT_TRACING_DISABLED;
f28f9e44
JG
1396 goto error;
1397 }
6e21424e
JR
1398 break;
1399 default:
4878de5c 1400 ret_code = LTTNG_ERR_UNKNOWN_DOMAIN;
6e21424e
JR
1401 goto error;
1402 }
1403
7972aab2 1404 switch (domain->type) {
2f77fc4b
DG
1405 case LTTNG_DOMAIN_KERNEL:
1406 {
1407 struct ltt_kernel_channel *kchan;
1408
28ab034a 1409 kchan = trace_kernel_get_channel_by_name(attr->name, session->kernel_session);
cd9adb8b 1410 if (kchan == nullptr) {
8cc65d5c
JR
1411 /*
1412 * Don't try to create a channel if the session has been started at
1413 * some point in time before. The tracer does not allow it.
1414 */
1415 if (session->has_been_started) {
4878de5c 1416 ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED;
8cc65d5c
JR
1417 goto error;
1418 }
1419
28ab034a 1420 if (session->snapshot.nb_output > 0 || session->snapshot_mode) {
54213acc 1421 /* Enforce mmap output for snapshot sessions. */
999af9c1 1422 attr->attr.output = LTTNG_EVENT_MMAP;
54213acc 1423 }
28ab034a 1424 ret_code = channel_kernel_create(session->kernel_session, attr, wpipe);
999af9c1 1425 if (attr->name[0] != '\0') {
85076754
MD
1426 session->kernel_session->has_non_default_channel = 1;
1427 }
2f77fc4b 1428 } else {
4878de5c 1429 ret_code = channel_kernel_enable(session->kernel_session, kchan);
2f77fc4b
DG
1430 }
1431
4878de5c 1432 if (ret_code != LTTNG_OK) {
2f77fc4b
DG
1433 goto error;
1434 }
1435
7d268848 1436 kernel_wait_quiescent();
2f77fc4b
DG
1437 break;
1438 }
1439 case LTTNG_DOMAIN_UST:
9232818f
JG
1440 case LTTNG_DOMAIN_JUL:
1441 case LTTNG_DOMAIN_LOG4J:
1442 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
1443 {
1444 struct ltt_ust_channel *uchan;
1445
9232818f
JG
1446 /*
1447 * FIXME
1448 *
1449 * Current agent implementation limitations force us to allow
1450 * only one channel at once in "agent" subdomains. Each
1451 * subdomain has a default channel name which must be strictly
1452 * adhered to.
1453 */
1454 if (domain->type == LTTNG_DOMAIN_JUL) {
5c7248cd
JG
1455 if (strncmp(attr->name,
1456 DEFAULT_JUL_CHANNEL_NAME,
1457 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
4878de5c 1458 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
9232818f
JG
1459 goto error;
1460 }
1461 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
5c7248cd
JG
1462 if (strncmp(attr->name,
1463 DEFAULT_LOG4J_CHANNEL_NAME,
1464 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
4878de5c 1465 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
9232818f
JG
1466 goto error;
1467 }
1468 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
28ab034a
JG
1469 if (strncmp(attr->name,
1470 DEFAULT_PYTHON_CHANNEL_NAME,
5c7248cd 1471 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
4878de5c 1472 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
9232818f
JG
1473 goto error;
1474 }
1475 }
1476
2f77fc4b
DG
1477 chan_ht = usess->domain_global.channels;
1478
999af9c1 1479 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
cd9adb8b 1480 if (uchan == nullptr) {
8cc65d5c
JR
1481 /*
1482 * Don't try to create a channel if the session has been started at
1483 * some point in time before. The tracer does not allow it.
1484 */
1485 if (session->has_been_started) {
4878de5c 1486 ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED;
8cc65d5c
JR
1487 goto error;
1488 }
1489
4878de5c 1490 ret_code = channel_ust_create(usess, attr, domain->buf_type);
999af9c1 1491 if (attr->name[0] != '\0') {
85076754
MD
1492 usess->has_non_default_channel = 1;
1493 }
2f77fc4b 1494 } else {
4878de5c 1495 ret_code = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1496 }
1497 break;
1498 }
2f77fc4b 1499 default:
4878de5c 1500 ret_code = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1501 goto error;
1502 }
1503
4878de5c 1504 if (ret_code == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) {
54213acc
JG
1505 session->has_non_mmap_channel = true;
1506 }
2f77fc4b 1507error:
1f345e94 1508end:
999af9c1 1509 lttng_channel_destroy(attr);
4878de5c 1510 return ret_code;
2f77fc4b
DG
1511}
1512
28ab034a
JG
1513enum lttng_error_code
1514cmd_process_attr_tracker_get_tracking_policy(struct ltt_session *session,
1515 enum lttng_domain_type domain,
1516 enum lttng_process_attr process_attr,
1517 enum lttng_tracking_policy *policy)
159b042f
JG
1518{
1519 enum lttng_error_code ret_code = LTTNG_OK;
1520 const struct process_attr_tracker *tracker;
1521
1522 switch (domain) {
1523 case LTTNG_DOMAIN_KERNEL:
1524 if (!session->kernel_session) {
1525 ret_code = LTTNG_ERR_INVALID;
1526 goto end;
1527 }
28ab034a 1528 tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr);
159b042f
JG
1529 break;
1530 case LTTNG_DOMAIN_UST:
1531 if (!session->ust_session) {
1532 ret_code = LTTNG_ERR_INVALID;
1533 goto end;
1534 }
28ab034a 1535 tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr);
159b042f
JG
1536 break;
1537 default:
1538 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1539 goto end;
1540 }
1541 if (tracker) {
1542 *policy = process_attr_tracker_get_tracking_policy(tracker);
1543 } else {
1544 ret_code = LTTNG_ERR_INVALID;
1545 }
1546end:
1547 return ret_code;
1548}
1549
28ab034a
JG
1550enum lttng_error_code
1551cmd_process_attr_tracker_set_tracking_policy(struct ltt_session *session,
1552 enum lttng_domain_type domain,
1553 enum lttng_process_attr process_attr,
1554 enum lttng_tracking_policy policy)
159b042f
JG
1555{
1556 enum lttng_error_code ret_code = LTTNG_OK;
1557
1558 switch (policy) {
1559 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1560 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1561 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1562 break;
1563 default:
1564 ret_code = LTTNG_ERR_INVALID;
1565 goto end;
1566 }
1567
1568 switch (domain) {
1569 case LTTNG_DOMAIN_KERNEL:
1570 if (!session->kernel_session) {
1571 ret_code = LTTNG_ERR_INVALID;
1572 goto end;
1573 }
1574 ret_code = kernel_process_attr_tracker_set_tracking_policy(
28ab034a 1575 session->kernel_session, process_attr, policy);
159b042f
JG
1576 break;
1577 case LTTNG_DOMAIN_UST:
1578 if (!session->ust_session) {
1579 ret_code = LTTNG_ERR_INVALID;
1580 goto end;
1581 }
1582 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
28ab034a 1583 session->ust_session, process_attr, policy);
159b042f
JG
1584 break;
1585 default:
1586 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1587 break;
1588 }
1589end:
1590 return ret_code;
1591}
1592
28ab034a
JG
1593enum lttng_error_code
1594cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session *session,
1595 enum lttng_domain_type domain,
1596 enum lttng_process_attr process_attr,
1597 const struct process_attr_value *value)
159b042f
JG
1598{
1599 enum lttng_error_code ret_code = LTTNG_OK;
1600
1601 switch (domain) {
1602 case LTTNG_DOMAIN_KERNEL:
1603 if (!session->kernel_session) {
1604 ret_code = LTTNG_ERR_INVALID;
1605 goto end;
1606 }
1607 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
28ab034a 1608 session->kernel_session, process_attr, value);
159b042f
JG
1609 break;
1610 case LTTNG_DOMAIN_UST:
1611 if (!session->ust_session) {
1612 ret_code = LTTNG_ERR_INVALID;
1613 goto end;
1614 }
1615 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
28ab034a 1616 session->ust_session, process_attr, value);
159b042f
JG
1617 break;
1618 default:
1619 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1620 break;
1621 }
1622end:
1623 return ret_code;
1624}
1625
28ab034a
JG
1626enum lttng_error_code
1627cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session *session,
1628 enum lttng_domain_type domain,
1629 enum lttng_process_attr process_attr,
1630 const struct process_attr_value *value)
159b042f
JG
1631{
1632 enum lttng_error_code ret_code = LTTNG_OK;
1633
1634 switch (domain) {
1635 case LTTNG_DOMAIN_KERNEL:
1636 if (!session->kernel_session) {
1637 ret_code = LTTNG_ERR_INVALID;
1638 goto end;
1639 }
1640 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
28ab034a 1641 session->kernel_session, process_attr, value);
159b042f
JG
1642 break;
1643 case LTTNG_DOMAIN_UST:
1644 if (!session->ust_session) {
1645 ret_code = LTTNG_ERR_INVALID;
1646 goto end;
1647 }
1648 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
28ab034a 1649 session->ust_session, process_attr, value);
159b042f
JG
1650 break;
1651 default:
1652 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1653 break;
1654 }
1655end:
1656 return ret_code;
1657}
1658
28ab034a
JG
1659enum lttng_error_code
1660cmd_process_attr_tracker_get_inclusion_set(struct ltt_session *session,
1661 enum lttng_domain_type domain,
1662 enum lttng_process_attr process_attr,
1663 struct lttng_process_attr_values **values)
159b042f
JG
1664{
1665 enum lttng_error_code ret_code = LTTNG_OK;
1666 const struct process_attr_tracker *tracker;
1667 enum process_attr_tracker_status status;
1668
1669 switch (domain) {
1670 case LTTNG_DOMAIN_KERNEL:
1671 if (!session->kernel_session) {
1672 ret_code = LTTNG_ERR_INVALID;
1673 goto end;
1674 }
28ab034a 1675 tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr);
159b042f
JG
1676 break;
1677 case LTTNG_DOMAIN_UST:
1678 if (!session->ust_session) {
1679 ret_code = LTTNG_ERR_INVALID;
1680 goto end;
1681 }
28ab034a 1682 tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr);
159b042f
JG
1683 break;
1684 default:
1685 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1686 goto end;
1687 }
1688
1689 if (!tracker) {
1690 ret_code = LTTNG_ERR_INVALID;
1691 goto end;
1692 }
1693
1694 status = process_attr_tracker_get_inclusion_set(tracker, values);
1695 switch (status) {
1696 case PROCESS_ATTR_TRACKER_STATUS_OK:
1697 ret_code = LTTNG_OK;
1698 break;
1699 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1700 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1701 break;
1702 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1703 ret_code = LTTNG_ERR_NOMEM;
1704 break;
1705 default:
1706 ret_code = LTTNG_ERR_UNK;
1707 break;
1708 }
1709
1710end:
1711 return ret_code;
1712}
1713
2f77fc4b
DG
1714/*
1715 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1716 */
8ddd72ef 1717int cmd_disable_event(struct command_ctx *cmd_ctx,
28ab034a
JG
1718 struct lttng_event *event,
1719 char *filter_expression,
1720 struct lttng_bytecode *bytecode,
1721 struct lttng_event_exclusion *exclusion)
2f77fc4b
DG
1722{
1723 int ret;
df4f5a87 1724 const char *event_name;
8ddd72ef
JR
1725 const struct ltt_session *session = cmd_ctx->session;
1726 const char *channel_name = cmd_ctx->lsm.u.disable.channel_name;
1727 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
6e911cad 1728
18a720cd
MD
1729 DBG("Disable event command for event \'%s\'", event->name);
1730
8ddd72ef
JR
1731 /*
1732 * Filter and exclusions are simply not handled by the
1733 * disable event command at this time.
1734 *
1735 * FIXME
1736 */
1737 (void) filter_expression;
1738 (void) exclusion;
1739
1740 /* Ignore the presence of filter or exclusion for the event */
1741 event->filter = 0;
1742 event->exclusion = 0;
1743
6e911cad
MD
1744 event_name = event->name;
1745
56047f5a
JG
1746 lttng::urcu::read_lock_guard read_lock;
1747
9b7431cf 1748 /* Error out on unhandled search criteria */
28ab034a
JG
1749 if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid ||
1750 event->filter || event->exclusion) {
7076b56e
JG
1751 ret = LTTNG_ERR_UNK;
1752 goto error;
6e911cad 1753 }
2f77fc4b
DG
1754
1755 switch (domain) {
1756 case LTTNG_DOMAIN_KERNEL:
1757 {
1758 struct ltt_kernel_channel *kchan;
1759 struct ltt_kernel_session *ksess;
1760
1761 ksess = session->kernel_session;
1762
85076754
MD
1763 /*
1764 * If a non-default channel has been created in the
1765 * session, explicitely require that -c chan_name needs
1766 * to be provided.
1767 */
1768 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1769 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1770 goto error_unlock;
85076754
MD
1771 }
1772
2f77fc4b 1773 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
cd9adb8b 1774 if (kchan == nullptr) {
f73fabfd 1775 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
7076b56e 1776 goto error_unlock;
2f77fc4b
DG
1777 }
1778
6e911cad
MD
1779 switch (event->type) {
1780 case LTTNG_EVENT_ALL:
9550ee81 1781 case LTTNG_EVENT_TRACEPOINT:
d0ae4ea8 1782 case LTTNG_EVENT_SYSCALL:
9550ee81
JR
1783 case LTTNG_EVENT_PROBE:
1784 case LTTNG_EVENT_FUNCTION:
28ab034a 1785 case LTTNG_EVENT_FUNCTION_ENTRY: /* fall-through */
9550ee81 1786 if (event_name[0] == '\0') {
cd9adb8b 1787 ret = event_kernel_disable_event(kchan, nullptr, event->type);
29c62722 1788 } else {
28ab034a 1789 ret = event_kernel_disable_event(kchan, event_name, event->type);
29c62722 1790 }
6e911cad 1791 if (ret != LTTNG_OK) {
7076b56e 1792 goto error_unlock;
6e911cad
MD
1793 }
1794 break;
6e911cad
MD
1795 default:
1796 ret = LTTNG_ERR_UNK;
7076b56e 1797 goto error_unlock;
2f77fc4b
DG
1798 }
1799
7d268848 1800 kernel_wait_quiescent();
2f77fc4b
DG
1801 break;
1802 }
1803 case LTTNG_DOMAIN_UST:
1804 {
1805 struct ltt_ust_channel *uchan;
1806 struct ltt_ust_session *usess;
1807
1808 usess = session->ust_session;
1809
7076b56e
JG
1810 if (validate_ust_event_name(event_name)) {
1811 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1812 goto error_unlock;
1813 }
1814
85076754
MD
1815 /*
1816 * If a non-default channel has been created in the
9550ee81 1817 * session, explicitly require that -c chan_name needs
85076754
MD
1818 * to be provided.
1819 */
1820 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1821 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1822 goto error_unlock;
85076754
MD
1823 }
1824
28ab034a 1825 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
cd9adb8b 1826 if (uchan == nullptr) {
f73fabfd 1827 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
7076b56e 1828 goto error_unlock;
2f77fc4b
DG
1829 }
1830
6e911cad
MD
1831 switch (event->type) {
1832 case LTTNG_EVENT_ALL:
b3639870
JR
1833 /*
1834 * An empty event name means that everything
1835 * should be disabled.
1836 */
1837 if (event->name[0] == '\0') {
1838 ret = event_ust_disable_all_tracepoints(usess, uchan);
77d536b2 1839 } else {
28ab034a 1840 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
77d536b2 1841 }
6e911cad 1842 if (ret != LTTNG_OK) {
7076b56e 1843 goto error_unlock;
6e911cad
MD
1844 }
1845 break;
1846 default:
1847 ret = LTTNG_ERR_UNK;
7076b56e 1848 goto error_unlock;
2f77fc4b
DG
1849 }
1850
28ab034a 1851 DBG3("Disable UST event %s in channel %s completed", event_name, channel_name);
2f77fc4b
DG
1852 break;
1853 }
5cdb6027 1854 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1855 case LTTNG_DOMAIN_JUL:
0e115563 1856 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1857 {
fefd409b 1858 struct agent *agt;
f20baf8e
DG
1859 struct ltt_ust_session *usess = session->ust_session;
1860
a0377dfe 1861 LTTNG_ASSERT(usess);
f20baf8e 1862
6e911cad
MD
1863 switch (event->type) {
1864 case LTTNG_EVENT_ALL:
1865 break;
1866 default:
1867 ret = LTTNG_ERR_UNK;
7076b56e 1868 goto error_unlock;
6e911cad
MD
1869 }
1870
5cdb6027 1871 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1872 if (!agt) {
1873 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
7076b56e 1874 goto error_unlock;
fefd409b 1875 }
b3639870
JR
1876 /*
1877 * An empty event name means that everything
1878 * should be disabled.
1879 */
1880 if (event->name[0] == '\0') {
18a720cd
MD
1881 ret = event_agent_disable_all(usess, agt);
1882 } else {
1883 ret = event_agent_disable(usess, agt, event_name);
1884 }
f20baf8e 1885 if (ret != LTTNG_OK) {
7076b56e 1886 goto error_unlock;
f20baf8e
DG
1887 }
1888
1889 break;
1890 }
2f77fc4b 1891 default:
f73fabfd 1892 ret = LTTNG_ERR_UND;
7076b56e 1893 goto error_unlock;
2f77fc4b
DG
1894 }
1895
f73fabfd 1896 ret = LTTNG_OK;
2f77fc4b 1897
7076b56e 1898error_unlock:
7076b56e 1899error:
8ddd72ef
JR
1900 free(exclusion);
1901 free(bytecode);
1902 free(filter_expression);
2f77fc4b
DG
1903 return ret;
1904}
1905
2f77fc4b
DG
1906/*
1907 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1908 */
26e1c61f 1909int cmd_add_context(struct command_ctx *cmd_ctx,
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
JR
1914 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
1915 const struct ltt_session *session = cmd_ctx->session;
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 */
26e1c61f 1923 if (cmd_ctx->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:
a0377dfe 1930 LTTNG_ASSERT(session->kernel_session);
979e618e
DG
1931
1932 if (session->kernel_session->channel_count == 0) {
1933 /* Create default channel */
cd9adb8b 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 */
28ab034a 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 {
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
JG
2009 struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name(
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
JG
2017 struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name(
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. */
28ab034a 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 */
88f06f15 2066static int _cmd_enable_event(struct ltt_session *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;
2f77fc4b 2078
a0377dfe
FD
2079 LTTNG_ASSERT(session);
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
9f449915
PP
2100 DBG("Enable event command for event \'%s\'", event->name);
2101
56047f5a 2102 lttng::urcu::read_lock_guard read_lock;
9f449915 2103
7972aab2 2104 switch (domain->type) {
2f77fc4b
DG
2105 case LTTNG_DOMAIN_KERNEL:
2106 {
2107 struct ltt_kernel_channel *kchan;
2108
85076754
MD
2109 /*
2110 * If a non-default channel has been created in the
2111 * session, explicitely require that -c chan_name needs
2112 * to be provided.
2113 */
28ab034a 2114 if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') {
85076754
MD
2115 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2116 goto error;
2117 }
2118
28ab034a 2119 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
cd9adb8b 2120 if (kchan == nullptr) {
28ab034a 2121 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL);
cd9adb8b 2122 if (attr == nullptr) {
f73fabfd 2123 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2124 goto error;
2125 }
28ab034a 2126 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
04c17253 2127 ret = LTTNG_ERR_INVALID;
04c17253
MD
2128 goto error;
2129 }
2f77fc4b 2130
28ab034a 2131 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
f73fabfd 2132 if (ret != LTTNG_OK) {
2f77fc4b
DG
2133 goto error;
2134 }
e5f5db7f 2135 channel_created = 1;
2f77fc4b
DG
2136 }
2137
2138 /* Get the newly created kernel channel pointer */
28ab034a 2139 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
cd9adb8b 2140 if (kchan == nullptr) {
2f77fc4b 2141 /* This sould not happen... */
f73fabfd 2142 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2143 goto error;
2144 }
2145
6e911cad
MD
2146 switch (event->type) {
2147 case LTTNG_EVENT_ALL:
29c62722 2148 {
cd9adb8b
JG
2149 char *filter_expression_a = nullptr;
2150 struct lttng_bytecode *filter_a = nullptr;
00a62084
MD
2151
2152 /*
2153 * We need to duplicate filter_expression and filter,
2154 * because ownership is passed to first enable
2155 * event.
2156 */
2157 if (filter_expression) {
2158 filter_expression_a = strdup(filter_expression);
2159 if (!filter_expression_a) {
2160 ret = LTTNG_ERR_FATAL;
2161 goto error;
2162 }
2163 }
2164 if (filter) {
64803277 2165 filter_a = zmalloc<lttng_bytecode>(sizeof(*filter_a) + filter->len);
00a62084
MD
2166 if (!filter_a) {
2167 free(filter_expression_a);
2168 ret = LTTNG_ERR_FATAL;
2169 goto error;
2170 }
2171 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2172 }
28ab034a
JG
2173 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2174 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
a969e101 2175 /* We have passed ownership */
cd9adb8b
JG
2176 filter_expression = nullptr;
2177 filter = nullptr;
29c62722
MD
2178 if (ret != LTTNG_OK) {
2179 if (channel_created) {
2180 /* Let's not leak a useless channel. */
2181 kernel_destroy_channel(kchan);
2182 }
00a62084
MD
2183 free(filter_expression_a);
2184 free(filter_a);
29c62722
MD
2185 goto error;
2186 }
28ab034a
JG
2187 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2188 ret = event_kernel_enable_event(
2189 kchan, event, filter_expression_a, filter_a);
60d21fa2 2190 /* We have passed ownership */
cd9adb8b
JG
2191 filter_expression_a = nullptr;
2192 filter_a = nullptr;
29c62722
MD
2193 if (ret != LTTNG_OK) {
2194 goto error;
2195 }
2196 break;
2197 }
6e6ef3d7 2198 case LTTNG_EVENT_PROBE:
dcabc190 2199 case LTTNG_EVENT_USERSPACE_PROBE:
6e6ef3d7
DG
2200 case LTTNG_EVENT_FUNCTION:
2201 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 2202 case LTTNG_EVENT_TRACEPOINT:
28ab034a 2203 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
a969e101 2204 /* We have passed ownership */
cd9adb8b
JG
2205 filter_expression = nullptr;
2206 filter = nullptr;
6e911cad
MD
2207 if (ret != LTTNG_OK) {
2208 if (channel_created) {
2209 /* Let's not leak a useless channel. */
2210 kernel_destroy_channel(kchan);
2211 }
2212 goto error;
e5f5db7f 2213 }
6e911cad
MD
2214 break;
2215 case LTTNG_EVENT_SYSCALL:
28ab034a 2216 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
a969e101 2217 /* We have passed ownership */
cd9adb8b
JG
2218 filter_expression = nullptr;
2219 filter = nullptr;
e2b957af
MD
2220 if (ret != LTTNG_OK) {
2221 goto error;
2222 }
6e911cad
MD
2223 break;
2224 default:
2225 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
2226 goto error;
2227 }
2228
7d268848 2229 kernel_wait_quiescent();
2f77fc4b
DG
2230 break;
2231 }
2232 case LTTNG_DOMAIN_UST:
2233 {
2234 struct ltt_ust_channel *uchan;
2235 struct ltt_ust_session *usess = session->ust_session;
2236
a0377dfe 2237 LTTNG_ASSERT(usess);
2f77fc4b 2238
85076754
MD
2239 /*
2240 * If a non-default channel has been created in the
2241 * session, explicitely require that -c chan_name needs
2242 * to be provided.
2243 */
2244 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2245 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2246 goto error;
2247 }
2248
2f77fc4b 2249 /* Get channel from global UST domain */
28ab034a 2250 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
cd9adb8b 2251 if (uchan == nullptr) {
2f77fc4b 2252 /* Create default channel */
28ab034a 2253 attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type);
cd9adb8b 2254 if (attr == nullptr) {
f73fabfd 2255 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2256 goto error;
2257 }
28ab034a 2258 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
04c17253 2259 ret = LTTNG_ERR_INVALID;
04c17253
MD
2260 goto error;
2261 }
2f77fc4b 2262
28ab034a 2263 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
f73fabfd 2264 if (ret != LTTNG_OK) {
2f77fc4b
DG
2265 goto error;
2266 }
2f77fc4b
DG
2267
2268 /* Get the newly created channel reference back */
28ab034a
JG
2269 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2270 channel_name);
a0377dfe 2271 LTTNG_ASSERT(uchan);
2f77fc4b
DG
2272 }
2273
141feb8c
JG
2274 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2275 /*
2276 * Don't allow users to add UST events to channels which
2277 * are assigned to a userspace subdomain (JUL, Log4J,
2278 * Python, etc.).
2279 */
2280 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2281 goto error;
2282 }
2283
dac8e046
JG
2284 if (!internal_event) {
2285 /*
2286 * Ensure the event name is not reserved for internal
2287 * use.
2288 */
2289 ret = validate_ust_event_name(event->name);
2290 if (ret) {
28ab034a 2291 WARN("Userspace event name %s failed validation.", event->name);
dac8e046
JG
2292 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2293 goto error;
2294 }
2295 }
2296
2f77fc4b 2297 /* At this point, the session and channel exist on the tracer */
28ab034a
JG
2298 ret = event_ust_enable_tracepoint(
2299 usess, uchan, event, filter_expression, filter, exclusion, internal_event);
49d21f93 2300 /* We have passed ownership */
cd9adb8b
JG
2301 filter_expression = nullptr;
2302 filter = nullptr;
2303 exclusion = nullptr;
94382e15
JG
2304 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2305 goto already_enabled;
2306 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2307 goto error;
2308 }
2309 break;
2310 }
5cdb6027 2311 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2312 case LTTNG_DOMAIN_JUL:
0e115563 2313 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2314 {
da6c3a50 2315 const char *default_event_name, *default_chan_name;
fefd409b 2316 struct agent *agt;
f20baf8e
DG
2317 struct lttng_event uevent;
2318 struct lttng_domain tmp_dom;
2319 struct ltt_ust_session *usess = session->ust_session;
2320
a0377dfe 2321 LTTNG_ASSERT(usess);
f20baf8e 2322
f28f9e44
JG
2323 if (!agent_tracing_is_enabled()) {
2324 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2325 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2326 goto error;
2327 }
2328
5cdb6027 2329 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2330 if (!agt) {
5cdb6027 2331 agt = agent_create(domain->type);
fefd409b 2332 if (!agt) {
e5b3c48c 2333 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2334 goto error;
2335 }
2336 agent_add(agt, usess->agents);
2337 }
2338
022d91ba 2339 /* Create the default tracepoint. */
996de3c7 2340 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2341 uevent.type = LTTNG_EVENT_TRACEPOINT;
2342 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
28ab034a 2343 default_event_name = event_get_default_agent_ust_name(domain->type);
da6c3a50 2344 if (!default_event_name) {
e5b3c48c 2345 ret = LTTNG_ERR_FATAL;
da6c3a50 2346 goto error;
f43f95a9 2347 }
da6c3a50 2348 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2349 uevent.name[sizeof(uevent.name) - 1] = '\0';
2350
2351 /*
2352 * The domain type is changed because we are about to enable the
2353 * default channel and event for the JUL domain that are hardcoded.
2354 * This happens in the UST domain.
2355 */
2356 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2357 tmp_dom.type = LTTNG_DOMAIN_UST;
2358
0e115563
DG
2359 switch (domain->type) {
2360 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2361 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2362 break;
2363 case LTTNG_DOMAIN_JUL:
da6c3a50 2364 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2365 break;
2366 case LTTNG_DOMAIN_PYTHON:
2367 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2368 break;
2369 default:
e98a44b0 2370 /* The switch/case we are in makes this impossible */
a0377dfe 2371 abort();
da6c3a50
DG
2372 }
2373
971da06a 2374 {
cd9adb8b
JG
2375 char *filter_expression_copy = nullptr;
2376 struct lttng_bytecode *filter_copy = nullptr;
971da06a
JG
2377
2378 if (filter) {
28ab034a
JG
2379 const size_t filter_size =
2380 sizeof(struct lttng_bytecode) + filter->len;
51755dc8 2381
64803277 2382 filter_copy = zmalloc<lttng_bytecode>(filter_size);
971da06a 2383 if (!filter_copy) {
018096a4 2384 ret = LTTNG_ERR_NOMEM;
b742e3e2 2385 goto error;
971da06a 2386 }
51755dc8 2387 memcpy(filter_copy, filter, filter_size);
971da06a 2388
28ab034a 2389 filter_expression_copy = strdup(filter_expression);
8404118c
JG
2390 if (!filter_expression) {
2391 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2392 }
2393
2394 if (!filter_expression_copy || !filter_copy) {
2395 free(filter_expression_copy);
2396 free(filter_copy);
2397 goto error;
8404118c 2398 }
971da06a
JG
2399 }
2400
28ab034a
JG
2401 ret = cmd_enable_event_internal(session,
2402 &tmp_dom,
2403 (char *) default_chan_name,
2404 &uevent,
2405 filter_expression_copy,
2406 filter_copy,
cd9adb8b 2407 nullptr,
28ab034a 2408 wpipe);
971da06a
JG
2409 }
2410
94382e15
JG
2411 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2412 goto already_enabled;
2413 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2414 goto error;
2415 }
2416
2417 /* The wild card * means that everything should be enabled. */
2418 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
28ab034a 2419 ret = event_agent_enable_all(usess, agt, event, filter, filter_expression);
f20baf8e 2420 } else {
28ab034a 2421 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
f20baf8e 2422 }
cd9adb8b
JG
2423 filter = nullptr;
2424 filter_expression = nullptr;
f20baf8e
DG
2425 if (ret != LTTNG_OK) {
2426 goto error;
2427 }
2428
2429 break;
2430 }
2f77fc4b 2431 default:
f73fabfd 2432 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2433 goto error;
2434 }
2435
f73fabfd 2436 ret = LTTNG_OK;
2f77fc4b 2437
94382e15 2438already_enabled:
2f77fc4b 2439error:
49d21f93
MD
2440 free(filter_expression);
2441 free(filter);
2442 free(exclusion);
cf0bcb51 2443 channel_attr_destroy(attr);
2f77fc4b
DG
2444 return ret;
2445}
2446
88f06f15
JG
2447/*
2448 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2449 * We own filter, exclusion, and filter_expression.
2450 */
8ddd72ef 2451int cmd_enable_event(struct command_ctx *cmd_ctx,
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 */
2472 ret = _cmd_enable_event(cmd_ctx->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 */
2492static int cmd_enable_event_internal(struct ltt_session *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{
28ab034a
JG
2501 return _cmd_enable_event(session,
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
e32d7f27 3127 session_lock_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 }
b178f53e
JG
3228 session_unlock_list();
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 *
3960 * The session list lock MUST be acquired before calling this function. Use
3961 * session_lock_list() and session_unlock_list().
3962 */
b178f53e 3963void cmd_list_lttng_sessions(struct lttng_session *sessions,
28ab034a
JG
3964 size_t session_count,
3965 uid_t uid,
3966 gid_t gid)
2f77fc4b
DG
3967{
3968 int ret;
3969 unsigned int i = 0;
3970 struct ltt_session *session;
3971 struct ltt_session_list *list = session_get_list();
28ab034a 3972 struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]);
2f77fc4b 3973
28ab034a 3974 DBG("Getting all available session for UID %d GID %d", uid, gid);
2f77fc4b
DG
3975 /*
3976 * Iterate over session list and append data after the control struct in
3977 * the buffer.
3978 */
28ab034a 3979 cds_list_for_each_entry (session, &list->head, list) {
e32d7f27
JG
3980 if (!session_get(session)) {
3981 continue;
3982 }
2f77fc4b
DG
3983 /*
3984 * Only list the sessions the user can control.
3985 */
28ab034a 3986 if (!session_access_ok(session, uid) || session->destroyed) {
e32d7f27 3987 session_put(session);
2f77fc4b
DG
3988 continue;
3989 }
3990
3991 struct ltt_kernel_session *ksess = session->kernel_session;
3992 struct ltt_ust_session *usess = session->ust_session;
3993
3994 if (session->consumer->type == CONSUMER_DST_NET ||
28ab034a
JG
3995 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3996 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3997 ret = build_network_session_path(
3998 sessions[i].path, sizeof(sessions[i].path), session);
2f77fc4b 3999 } else {
28ab034a
JG
4000 ret = snprintf(sessions[i].path,
4001 sizeof(sessions[i].path),
4002 "%s",
4003 session->consumer->dst.session_root_path);
2f77fc4b
DG
4004 }
4005 if (ret < 0) {
4006 PERROR("snprintf session path");
e32d7f27 4007 session_put(session);
2f77fc4b
DG
4008 continue;
4009 }
4010
4011 strncpy(sessions[i].name, session->name, NAME_MAX);
4012 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 4013 sessions[i].enabled = session->active;
2cbf8fed 4014 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 4015 sessions[i].live_timer_interval = session->live_timer;
b178f53e
JG
4016 extended[i].creation_time.value = (uint64_t) session->creation_time;
4017 extended[i].creation_time.is_set = 1;
2f77fc4b 4018 i++;
e32d7f27 4019 session_put(session);
2f77fc4b
DG
4020 }
4021}
4022
806e2684 4023/*
6d805429 4024 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 4025 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 4026 */
6d805429 4027int cmd_data_pending(struct ltt_session *session)
806e2684
DG
4028{
4029 int ret;
4030 struct ltt_kernel_session *ksess = session->kernel_session;
4031 struct ltt_ust_session *usess = session->ust_session;
4032
a0377dfe 4033 LTTNG_ASSERT(session);
806e2684 4034
5c408ad8
JD
4035 DBG("Data pending for session %s", session->name);
4036
806e2684 4037 /* Session MUST be stopped to ask for data availability. */
8382cf6f 4038 if (session->active) {
806e2684
DG
4039 ret = LTTNG_ERR_SESSION_STARTED;
4040 goto error;
3a89d11a
DG
4041 } else {
4042 /*
4043 * If stopped, just make sure we've started before else the above call
4044 * will always send that there is data pending.
4045 *
4046 * The consumer assumes that when the data pending command is received,
4047 * the trace has been started before or else no output data is written
4048 * by the streams which is a condition for data pending. So, this is
4049 * *VERY* important that we don't ask the consumer before a start
4050 * trace.
4051 */
8382cf6f 4052 if (!session->has_been_started) {
3a89d11a
DG
4053 ret = 0;
4054 goto error;
4055 }
806e2684
DG
4056 }
4057
92816cc3
JG
4058 /* A rotation is still pending, we have to wait. */
4059 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5c408ad8
JD
4060 DBG("Rotate still pending for session %s", session->name);
4061 ret = 1;
4062 goto error;
4063 }
4064
806e2684 4065 if (ksess && ksess->consumer) {
6d805429
DG
4066 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
4067 if (ret == 1) {
806e2684
DG
4068 /* Data is still being extracted for the kernel. */
4069 goto error;
4070 }
4071 }
4072
4073 if (usess && usess->consumer) {
6d805429
DG
4074 ret = consumer_is_data_pending(usess->id, usess->consumer);
4075 if (ret == 1) {
806e2684
DG
4076 /* Data is still being extracted for the kernel. */
4077 goto error;
4078 }
4079 }
4080
4081 /* Data is ready to be read by a viewer */
6d805429 4082 ret = 0;
806e2684
DG
4083
4084error:
4085 return ret;
4086}
4087
6dc3064a
DG
4088/*
4089 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4090 *
4091 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4092 */
4093int cmd_snapshot_add_output(struct ltt_session *session,
28ab034a
JG
4094 const struct lttng_snapshot_output *output,
4095 uint32_t *id)
6dc3064a
DG
4096{
4097 int ret;
4098 struct snapshot_output *new_output;
4099
a0377dfe
FD
4100 LTTNG_ASSERT(session);
4101 LTTNG_ASSERT(output);
6dc3064a
DG
4102
4103 DBG("Cmd snapshot add output for session %s", session->name);
4104
4105 /*
903ef685 4106 * Can't create an output if the session is not set in no-output mode.
6dc3064a
DG
4107 */
4108 if (session->output_traces) {
903ef685 4109 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
4110 goto error;
4111 }
4112
54213acc
JG
4113 if (session->has_non_mmap_channel) {
4114 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
4115 goto error;
4116 }
4117
6dc3064a
DG
4118 /* Only one output is allowed until we have the "tee" feature. */
4119 if (session->snapshot.nb_output == 1) {
4120 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
4121 goto error;
4122 }
4123
4124 new_output = snapshot_output_alloc();
4125 if (!new_output) {
4126 ret = LTTNG_ERR_NOMEM;
4127 goto error;
4128 }
4129
28ab034a
JG
4130 ret = snapshot_output_init(session,
4131 output->max_size,
4132 output->name,
4133 output->ctrl_url,
4134 output->data_url,
4135 session->consumer,
4136 new_output,
4137 &session->snapshot);
6dc3064a
DG
4138 if (ret < 0) {
4139 if (ret == -ENOMEM) {
4140 ret = LTTNG_ERR_NOMEM;
4141 } else {
4142 ret = LTTNG_ERR_INVALID;
4143 }
4144 goto free_error;
4145 }
4146
6dc3064a
DG
4147 snapshot_add_output(&session->snapshot, new_output);
4148 if (id) {
4149 *id = new_output->id;
4150 }
6dc3064a
DG
4151
4152 return LTTNG_OK;
4153
4154free_error:
4155 snapshot_output_destroy(new_output);
4156error:
4157 return ret;
4158}
4159
4160/*
4161 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4162 *
4163 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4164 */
28ab034a 4165int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output)
6dc3064a
DG
4166{
4167 int ret;
cd9adb8b 4168 struct snapshot_output *sout = nullptr;
6dc3064a 4169
a0377dfe
FD
4170 LTTNG_ASSERT(session);
4171 LTTNG_ASSERT(output);
6dc3064a 4172
56047f5a 4173 lttng::urcu::read_lock_guard read_lock;
6dc3064a
DG
4174
4175 /*
d3f14b8a
MD
4176 * Permission denied to create an output if the session is not
4177 * set in no output mode.
6dc3064a
DG
4178 */
4179 if (session->output_traces) {
903ef685 4180 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
4181 goto error;
4182 }
4183
eb240553 4184 if (output->id) {
28ab034a
JG
4185 DBG("Cmd snapshot del output id %" PRIu32 " for session %s",
4186 output->id,
4187 session->name);
eb240553
DG
4188 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
4189 } else if (*output->name != '\0') {
28ab034a 4190 DBG("Cmd snapshot del output name %s for session %s", output->name, session->name);
eb240553
DG
4191 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
4192 }
6dc3064a
DG
4193 if (!sout) {
4194 ret = LTTNG_ERR_INVALID;
4195 goto error;
4196 }
4197
4198 snapshot_delete_output(&session->snapshot, sout);
4199 snapshot_output_destroy(sout);
4200 ret = LTTNG_OK;
4201
4202error:
6dc3064a
DG
4203 return ret;
4204}
4205
4206/*
4207 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4208 *
4209 * If no output is available, outputs is untouched and 0 is returned.
4210 *
4211 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4212 */
4213ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
28ab034a 4214 struct lttng_snapshot_output **outputs)
6dc3064a
DG
4215{
4216 int ret, idx = 0;
cd9adb8b 4217 struct lttng_snapshot_output *list = nullptr;
6dc3064a
DG
4218 struct lttng_ht_iter iter;
4219 struct snapshot_output *output;
4220
a0377dfe
FD
4221 LTTNG_ASSERT(session);
4222 LTTNG_ASSERT(outputs);
6dc3064a
DG
4223
4224 DBG("Cmd snapshot list outputs for session %s", session->name);
4225
4226 /*
d3f14b8a
MD
4227 * Permission denied to create an output if the session is not
4228 * set in no output mode.
6dc3064a
DG
4229 */
4230 if (session->output_traces) {
903ef685
JG
4231 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4232 goto end;
6dc3064a
DG
4233 }
4234
4235 if (session->snapshot.nb_output == 0) {
4236 ret = 0;
903ef685 4237 goto end;
6dc3064a
DG
4238 }
4239
64803277 4240 list = calloc<lttng_snapshot_output>(session->snapshot.nb_output);
6dc3064a 4241 if (!list) {
b223ca94 4242 ret = -LTTNG_ERR_NOMEM;
903ef685 4243 goto end;
6dc3064a
DG
4244 }
4245
4246 /* Copy list from session to the new list object. */
56047f5a
JG
4247 {
4248 lttng::urcu::read_lock_guard read_lock;
4249
4250 cds_lfht_for_each_entry (
4251 session->snapshot.output_ht->ht, &iter.iter, output, node.node) {
4252 LTTNG_ASSERT(output->consumer);
4253 list[idx].id = output->id;
4254 list[idx].max_size = output->max_size;
4255 if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) {
6ce22875 4256 ret = -LTTNG_ERR_INVALID;
903ef685 4257 goto error;
6ce22875 4258 }
6dc3064a 4259
56047f5a
JG
4260 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4261 if (lttng_strncpy(list[idx].ctrl_url,
4262 output->consumer->dst.session_root_path,
4263 sizeof(list[idx].ctrl_url))) {
4264 ret = -LTTNG_ERR_INVALID;
4265 goto error;
4266 }
4267 } else {
4268 /* Control URI. */
4269 ret = uri_to_str_url(&output->consumer->dst.net.control,
4270 list[idx].ctrl_url,
4271 sizeof(list[idx].ctrl_url));
4272 if (ret < 0) {
4273 ret = -LTTNG_ERR_NOMEM;
4274 goto error;
4275 }
4276
4277 /* Data URI. */
4278 ret = uri_to_str_url(&output->consumer->dst.net.data,
4279 list[idx].data_url,
4280 sizeof(list[idx].data_url));
4281 if (ret < 0) {
4282 ret = -LTTNG_ERR_NOMEM;
4283 goto error;
4284 }
6dc3064a 4285 }
56047f5a
JG
4286
4287 idx++;
6dc3064a 4288 }
6dc3064a
DG
4289 }
4290
4291 *outputs = list;
cd9adb8b 4292 list = nullptr;
b223ca94 4293 ret = session->snapshot.nb_output;
6dc3064a 4294error:
b223ca94 4295 free(list);
903ef685 4296end:
b223ca94 4297 return ret;
6dc3064a
DG
4298}
4299
93ec662e
JD
4300/*
4301 * Check if we can regenerate the metadata for this session.
4302 * Only kernel, UST per-uid and non-live sessions are supported.
4303 *
4304 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4305 */
28ab034a 4306static int check_regenerate_metadata_support(struct ltt_session *session)
93ec662e
JD
4307{
4308 int ret;
4309
a0377dfe 4310 LTTNG_ASSERT(session);
93ec662e
JD
4311
4312 if (session->live_timer != 0) {
4313 ret = LTTNG_ERR_LIVE_SESSION;
4314 goto end;
4315 }
4316 if (!session->active) {
4317 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4318 goto end;
4319 }
4320 if (session->ust_session) {
4321 switch (session->ust_session->buffer_type) {
4322 case LTTNG_BUFFER_PER_UID:
4323 break;
4324 case LTTNG_BUFFER_PER_PID:
4325 ret = LTTNG_ERR_PER_PID_SESSION;
4326 goto end;
4327 default:
a0377dfe 4328 abort();
93ec662e
JD
4329 ret = LTTNG_ERR_UNK;
4330 goto end;
4331 }
4332 }
4333 if (session->consumer->type == CONSUMER_DST_NET &&
28ab034a 4334 session->consumer->relay_minor_version < 8) {
93ec662e
JD
4335 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4336 goto end;
4337 }
4338 ret = 0;
4339
4340end:
4341 return ret;
4342}
4343
93ec662e 4344/*
eded6438 4345 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
93ec662e
JD
4346 *
4347 * Ask the consumer to truncate the existing metadata file(s) and
4348 * then regenerate the metadata. Live and per-pid sessions are not
4349 * supported and return an error.
4350 *
1136f41b 4351 * Return LTTNG_OK on success or else a LTTNG_ERR code.
93ec662e 4352 */
eded6438 4353int cmd_regenerate_metadata(struct ltt_session *session)
93ec662e
JD
4354{
4355 int ret;
4356
a0377dfe 4357 LTTNG_ASSERT(session);
93ec662e 4358
eded6438 4359 ret = check_regenerate_metadata_support(session);
93ec662e
JD
4360 if (ret) {
4361 goto end;
4362 }
4363
4364 if (session->kernel_session) {
28ab034a 4365 ret = kernctl_session_regenerate_metadata(session->kernel_session->fd);
93ec662e
JD
4366 if (ret < 0) {
4367 ERR("Failed to regenerate the kernel metadata");
4368 goto end;
4369 }
4370 }
4371
4372 if (session->ust_session) {
d7bfb9b0 4373 ret = trace_ust_regenerate_metadata(session->ust_session);
93ec662e
JD
4374 if (ret < 0) {
4375 ERR("Failed to regenerate the UST metadata");
4376 goto end;
4377 }
4378 }
4379 DBG("Cmd metadata regenerate for session %s", session->name);
4380 ret = LTTNG_OK;
4381
4382end:
4383 return ret;
4384}
4385
c2561365
JD
4386/*
4387 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4388 *
4389 * Ask the tracer to regenerate a new statedump.
4390 *
1136f41b 4391 * Return LTTNG_OK on success or else a LTTNG_ERR code.
c2561365
JD
4392 */
4393int cmd_regenerate_statedump(struct ltt_session *session)
4394{
4395 int ret;
4396
a0377dfe 4397 LTTNG_ASSERT(session);
c2561365
JD
4398
4399 if (!session->active) {
4400 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4401 goto end;
4402 }
c2561365
JD
4403
4404 if (session->kernel_session) {
28ab034a 4405 ret = kernctl_session_regenerate_statedump(session->kernel_session->fd);
c2561365
JD
4406 /*
4407 * Currently, the statedump in kernel can only fail if out
4408 * of memory.
4409 */
4410 if (ret < 0) {
4411 if (ret == -ENOMEM) {
4412 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4413 } else {
4414 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4415 }
4416 ERR("Failed to regenerate the kernel statedump");
4417 goto end;
4418 }
4419 }
4420
4421 if (session->ust_session) {
4422 ret = ust_app_regenerate_statedump_all(session->ust_session);
4423 /*
4424 * Currently, the statedump in UST always returns 0.
4425 */
4426 if (ret < 0) {
4427 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4428 ERR("Failed to regenerate the UST statedump");
4429 goto end;
4430 }
4431 }
4432 DBG("Cmd regenerate statedump for session %s", session->name);
4433 ret = LTTNG_OK;
4434
4435end:
4436 return ret;
4437}
4438
28ab034a
JG
4439static enum lttng_error_code
4440synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread,
4441 struct lttng_trigger *trigger,
4442 const struct lttng_credentials *cmd_creds)
70670472 4443{
989a0844 4444 enum lttng_error_code ret_code;
28ab034a 4445 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
989a0844
FD
4446 const char *trigger_name;
4447 uid_t trigger_owner;
4448 enum lttng_trigger_status trigger_status;
4449 const enum lttng_domain_type trigger_domain =
28ab034a 4450 lttng_trigger_get_underlying_domain_type_restriction(trigger);
70670472 4451
989a0844 4452 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4453 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
989a0844 4454
a0377dfe
FD
4455 LTTNG_ASSERT(condition);
4456 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
28ab034a 4457 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
989a0844
FD
4458
4459 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
28ab034a 4460 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
989a0844
FD
4461
4462 session_lock_list();
4463 switch (trigger_domain) {
4464 case LTTNG_DOMAIN_KERNEL:
4465 {
4466 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4467 if (ret_code != LTTNG_OK) {
4468 enum lttng_error_code notif_thread_unregister_ret;
4469
4470 notif_thread_unregister_ret =
28ab034a
JG
4471 notification_thread_command_unregister_trigger(notification_thread,
4472 trigger);
989a0844
FD
4473
4474 if (notif_thread_unregister_ret != LTTNG_OK) {
4475 /* Return the original error code. */
4476 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4477 trigger_name,
4478 (int) trigger_owner,
4479 ret_code);
989a0844 4480 }
7ebbf3f0
JG
4481
4482 goto end_unlock_session_list;
989a0844
FD
4483 }
4484 break;
70670472 4485 }
989a0844
FD
4486 case LTTNG_DOMAIN_UST:
4487 ust_app_global_update_all_event_notifier_rules();
4488 break;
4489 case LTTNG_DOMAIN_JUL:
4490 case LTTNG_DOMAIN_LOG4J:
4491 case LTTNG_DOMAIN_PYTHON:
4492 {
4493 /* Agent domains. */
28ab034a 4494 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
70670472 4495
989a0844
FD
4496 if (!agt) {
4497 agt = agent_create(trigger_domain);
4498 if (!agt) {
4499 ret_code = LTTNG_ERR_NOMEM;
4500 goto end_unlock_session_list;
4501 }
4502
412d7227 4503 agent_add(agt, the_trigger_agents_ht_by_domain);
989a0844
FD
4504 }
4505
7966af57 4506 ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
989a0844
FD
4507 if (ret_code != LTTNG_OK) {
4508 goto end_unlock_session_list;
4509 }
4510
4511 break;
4512 }
4513 case LTTNG_DOMAIN_NONE:
4514 default:
4515 abort();
4516 }
4517
4518 ret_code = LTTNG_OK;
4519end_unlock_session_list:
4520 session_unlock_list();
70670472
JR
4521 return ret_code;
4522}
4523
4524enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
28ab034a
JG
4525 struct lttng_trigger *trigger,
4526 bool is_trigger_anonymous,
4527 struct notification_thread_handle *notification_thread,
4528 struct lttng_trigger **return_trigger)
b0880ae5 4529{
70670472 4530 enum lttng_error_code ret_code;
70670472
JR
4531 const char *trigger_name;
4532 uid_t trigger_owner;
4533 enum lttng_trigger_status trigger_status;
4534
4535 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
28ab034a 4536 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
ce0b1d61 4537
28ab034a 4538 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4539 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
70670472
JR
4540
4541 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4542 trigger_name,
4543 (int) trigger_owner,
4544 (int) lttng_credentials_get_uid(cmd_creds));
b0880ae5 4545
64eafdf6
JR
4546 /*
4547 * Validate the trigger credentials against the command credentials.
4548 * Only the root user can register a trigger with non-matching
4549 * credentials.
4550 */
28ab034a 4551 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
746e08d7 4552 if (lttng_credentials_get_uid(cmd_creds) != 0) {
70670472 4553 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4554 trigger_name,
4555 (int) trigger_owner,
4556 (int) lttng_credentials_get_uid(cmd_creds));
70670472 4557 ret_code = LTTNG_ERR_INVALID_TRIGGER;
64eafdf6
JR
4558 goto end;
4559 }
4560 }
3da864a9 4561
58daac01
JR
4562 /*
4563 * The bytecode generation also serves as a validation step for the
4564 * bytecode expressions.
4565 */
70670472
JR
4566 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4567 if (ret_code != LTTNG_OK) {
4568 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4569 trigger_name,
4570 (int) trigger_owner,
4571 ret_code);
58daac01
JR
4572 goto end;
4573 }
4574
242388e4
JR
4575 /*
4576 * A reference to the trigger is acquired by the notification thread.
4577 * It is safe to return the same trigger to the caller since it the
4578 * other user holds a reference.
4579 *
4580 * The trigger is modified during the execution of the
4581 * "register trigger" command. However, by the time the command returns,
4582 * it is safe to use without any locking as its properties are
4583 * immutable.
4584 */
0efb2ad7 4585 ret_code = notification_thread_command_register_trigger(
28ab034a 4586 notification_thread, trigger, is_trigger_anonymous);
70670472 4587 if (ret_code != LTTNG_OK) {
ce0b1d61 4588 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4589 trigger_name,
4590 (int) trigger_owner,
4591 ret_code);
44760c20 4592 goto end;
70670472
JR
4593 }
4594
ce0b1d61 4595 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
28ab034a 4596 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
ce0b1d61 4597
70670472
JR
4598 /*
4599 * Synchronize tracers if the trigger adds an event notifier.
4600 */
989a0844 4601 if (lttng_trigger_needs_tracer_notifier(trigger)) {
28ab034a
JG
4602 ret_code = synchronize_tracer_notifier_register(
4603 notification_thread, trigger, cmd_creds);
989a0844 4604 if (ret_code != LTTNG_OK) {
28ab034a 4605 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
989a0844 4606 goto end;
70670472
JR
4607 }
4608 }
4609
746e08d7
JG
4610 /*
4611 * Return an updated trigger to the client.
4612 *
4613 * Since a modified version of the same trigger is returned, acquire a
4614 * reference to the trigger so the caller doesn't have to care if those
4615 * are distinct instances or not.
4616 */
39b95a70
JG
4617 if (ret_code == LTTNG_OK) {
4618 lttng_trigger_get(trigger);
4619 *return_trigger = trigger;
4620 /* Ownership of trigger was transferred to caller. */
cd9adb8b 4621 trigger = nullptr;
39b95a70 4622 }
b0880ae5 4623end:
70670472 4624 return ret_code;
989a0844
FD
4625}
4626
28ab034a
JG
4627static enum lttng_error_code
4628synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
989a0844
FD
4629{
4630 enum lttng_error_code ret_code;
28ab034a 4631 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
989a0844 4632 const enum lttng_domain_type trigger_domain =
28ab034a 4633 lttng_trigger_get_underlying_domain_type_restriction(trigger);
989a0844 4634
a0377dfe
FD
4635 LTTNG_ASSERT(condition);
4636 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
28ab034a 4637 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
989a0844
FD
4638
4639 session_lock_list();
4640 switch (trigger_domain) {
4641 case LTTNG_DOMAIN_KERNEL:
4642 ret_code = kernel_unregister_event_notifier(trigger);
e689039f
JG
4643 if (ret_code != LTTNG_OK) {
4644 goto end_unlock_session_list;
4645 }
4646
989a0844
FD
4647 break;
4648 case LTTNG_DOMAIN_UST:
4649 ust_app_global_update_all_event_notifier_rules();
4650 break;
4651 case LTTNG_DOMAIN_JUL:
4652 case LTTNG_DOMAIN_LOG4J:
4653 case LTTNG_DOMAIN_PYTHON:
4654 {
4655 /* Agent domains. */
28ab034a 4656 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
989a0844 4657
566190c4
JG
4658 /*
4659 * This trigger was never registered in the first place. Calling
4660 * this function under those circumstances is an internal error.
4661 */
a0377dfe 4662 LTTNG_ASSERT(agt);
7966af57 4663 ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
989a0844
FD
4664 if (ret_code != LTTNG_OK) {
4665 goto end_unlock_session_list;
4666 }
4667
4668 break;
4669 }
4670 case LTTNG_DOMAIN_NONE:
4671 default:
4672 abort();
4673 }
4674
4675 ret_code = LTTNG_OK;
4676
9b7cbebd
JG
4677end_unlock_session_list:
4678 session_unlock_list();
4679 return ret_code;
b0880ae5
JG
4680}
4681
70670472 4682enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
28ab034a
JG
4683 const struct lttng_trigger *trigger,
4684 struct notification_thread_handle *notification_thread)
b0880ae5 4685{
70670472 4686 enum lttng_error_code ret_code;
70670472
JR
4687 const char *trigger_name;
4688 uid_t trigger_owner;
4689 enum lttng_trigger_status trigger_status;
cd9adb8b 4690 struct lttng_trigger *sessiond_trigger = nullptr;
70670472
JR
4691
4692 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
0efb2ad7 4693 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
989a0844 4694 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4695 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
70670472
JR
4696
4697 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4698 trigger_name,
4699 (int) trigger_owner,
4700 (int) lttng_credentials_get_uid(cmd_creds));
b0880ae5 4701
64eafdf6
JR
4702 /*
4703 * Validate the trigger credentials against the command credentials.
4704 * Only the root user can unregister a trigger with non-matching
4705 * credentials.
4706 */
28ab034a 4707 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
746e08d7 4708 if (lttng_credentials_get_uid(cmd_creds) != 0) {
70670472 4709 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4710 trigger_name,
4711 (int) trigger_owner,
4712 (int) lttng_credentials_get_uid(cmd_creds));
70670472 4713 ret_code = LTTNG_ERR_INVALID_TRIGGER;
64eafdf6
JR
4714 goto end;
4715 }
4716 }
3da864a9 4717
5c5373c3
JR
4718 /* Fetch the sessiond side trigger object. */
4719 ret_code = notification_thread_command_get_trigger(
28ab034a 4720 notification_thread, trigger, &sessiond_trigger);
5c5373c3
JR
4721 if (ret_code != LTTNG_OK) {
4722 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4723 trigger_name,
4724 (int) trigger_owner,
4725 ret_code);
5c5373c3
JR
4726 goto end;
4727 }
4728
a0377dfe 4729 LTTNG_ASSERT(sessiond_trigger);
5c5373c3
JR
4730
4731 /*
4732 * From this point on, no matter what, consider the trigger
4733 * unregistered.
4734 *
4735 * We set the unregistered state of the sessiond side trigger object in
4736 * the client thread since we want to minimize the possibility of the
4737 * notification thread being stalled due to a long execution of an
4738 * action that required the trigger lock.
4739 */
4740 lttng_trigger_set_as_unregistered(sessiond_trigger);
4741
28ab034a 4742 ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger);
70670472 4743 if (ret_code != LTTNG_OK) {
ce0b1d61 4744 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4745 trigger_name,
4746 (int) trigger_owner,
4747 ret_code);
13839b27 4748 goto end;
70670472
JR
4749 }
4750
4751 /*
4752 * Synchronize tracers if the trigger removes an event notifier.
44760c20
JR
4753 * Do this even if the trigger unregistration failed to at least stop
4754 * the tracers from producing notifications associated with this
4755 * event notifier.
70670472 4756 */
989a0844
FD
4757 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4758 ret_code = synchronize_tracer_notifier_unregister(trigger);
4759 if (ret_code != LTTNG_OK) {
4760 ERR("Error unregistering trigger to tracer.");
4761 goto end;
70670472
JR
4762 }
4763 }
4764
b0880ae5 4765end:
5c5373c3 4766 lttng_trigger_put(sessiond_trigger);
70670472 4767 return ret_code;
989a0844 4768}
b0880ae5 4769
ddd915a3 4770enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
28ab034a
JG
4771 struct notification_thread_handle *notification_thread,
4772 struct lttng_triggers **return_triggers)
fbc9f37d 4773{
f2bda80e 4774 int ret;
fbc9f37d 4775 enum lttng_error_code ret_code;
cd9adb8b 4776 struct lttng_triggers *triggers = nullptr;
fbc9f37d
JR
4777
4778 /* Get the set of triggers from the notification thread. */
4779 ret_code = notification_thread_command_list_triggers(
28ab034a 4780 notification_thread, cmd_ctx->creds.uid, &triggers);
fbc9f37d 4781 if (ret_code != LTTNG_OK) {
fbc9f37d
JR
4782 goto end;
4783 }
4784
f2bda80e
JG
4785 ret = lttng_triggers_remove_hidden_triggers(triggers);
4786 if (ret) {
4787 ret_code = LTTNG_ERR_UNK;
4788 goto end;
4789 }
4790
fbc9f37d 4791 *return_triggers = triggers;
cd9adb8b 4792 triggers = nullptr;
ddd915a3 4793 ret_code = LTTNG_OK;
fbc9f37d
JR
4794end:
4795 lttng_triggers_destroy(triggers);
ddd915a3 4796 return ret_code;
fbc9f37d 4797}
588c4b0d 4798
28ab034a
JG
4799enum lttng_error_code
4800cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4801 const struct lttng_error_query *query,
4802 struct lttng_error_query_results **_results,
4803 struct notification_thread_handle *notification_thread)
588c4b0d
JG
4804{
4805 enum lttng_error_code ret_code;
4806 const struct lttng_trigger *query_target_trigger;
cd9adb8b
JG
4807 const struct lttng_action *query_target_action = nullptr;
4808 struct lttng_trigger *matching_trigger = nullptr;
588c4b0d
JG
4809 const char *trigger_name;
4810 uid_t trigger_owner;
4811 enum lttng_trigger_status trigger_status;
cd9adb8b 4812 struct lttng_error_query_results *results = nullptr;
588c4b0d
JG
4813
4814 switch (lttng_error_query_get_target_type(query)) {
4815 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4816 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4817 break;
63dd3d7b 4818 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
28ab034a 4819 query_target_trigger = lttng_error_query_condition_borrow_target(query);
63dd3d7b 4820 break;
588c4b0d 4821 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
28ab034a 4822 query_target_trigger = lttng_error_query_action_borrow_trigger_target(query);
588c4b0d
JG
4823 break;
4824 default:
4825 abort();
4826 }
4827
a0377dfe 4828 LTTNG_ASSERT(query_target_trigger);
588c4b0d 4829
28ab034a
JG
4830 ret_code = notification_thread_command_get_trigger(
4831 notification_thread, query_target_trigger, &matching_trigger);
588c4b0d
JG
4832 if (ret_code != LTTNG_OK) {
4833 goto end;
4834 }
4835
4836 /* No longer needed. */
cd9adb8b 4837 query_target_trigger = nullptr;
588c4b0d 4838
28ab034a 4839 if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
588c4b0d
JG
4840 /* Get the sessiond-side version of the target action. */
4841 query_target_action =
28ab034a 4842 lttng_error_query_action_borrow_action_target(query, matching_trigger);
588c4b0d
JG
4843 }
4844
4845 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
28ab034a
JG
4846 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4847 trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner);
a0377dfe 4848 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
588c4b0d
JG
4849
4850 results = lttng_error_query_results_create();
4851 if (!results) {
4852 ret_code = LTTNG_ERR_NOMEM;
4853 goto end;
4854 }
4855
4856 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4857 trigger_name,
4858 (int) trigger_owner,
4859 (int) lttng_credentials_get_uid(cmd_creds));
588c4b0d
JG
4860
4861 /*
4862 * Validate the trigger credentials against the command credentials.
4863 * Only the root user can target a trigger with non-matching
4864 * credentials.
4865 */
28ab034a
JG
4866 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger),
4867 cmd_creds)) {
588c4b0d
JG
4868 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4869 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4870 trigger_name,
4871 (int) trigger_owner,
4872 (int) lttng_credentials_get_uid(cmd_creds));
588c4b0d
JG
4873 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4874 goto end;
4875 }
4876 }
4877
4878 switch (lttng_error_query_get_target_type(query)) {
4879 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
28ab034a 4880 trigger_status = lttng_trigger_add_error_results(matching_trigger, results);
588c4b0d
JG
4881
4882 switch (trigger_status) {
4883 case LTTNG_TRIGGER_STATUS_OK:
4884 break;
4885 default:
4886 ret_code = LTTNG_ERR_UNK;
4887 goto end;
4888 }
4889
4890 break;
63dd3d7b
JG
4891 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4892 {
28ab034a
JG
4893 trigger_status =
4894 lttng_trigger_condition_add_error_results(matching_trigger, results);
63dd3d7b
JG
4895
4896 switch (trigger_status) {
4897 case LTTNG_TRIGGER_STATUS_OK:
4898 break;
4899 default:
4900 ret_code = LTTNG_ERR_UNK;
4901 goto end;
4902 }
4903
4904 break;
4905 }
588c4b0d
JG
4906 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4907 {
4908 const enum lttng_action_status action_status =
28ab034a 4909 lttng_action_add_error_query_results(query_target_action, results);
588c4b0d
JG
4910
4911 switch (action_status) {
4912 case LTTNG_ACTION_STATUS_OK:
4913 break;
4914 default:
4915 ret_code = LTTNG_ERR_UNK;
4916 goto end;
4917 }
4918
4919 break;
4920 }
4921 default:
ef4cf1d2 4922 abort();
588c4b0d
JG
4923 break;
4924 }
4925
4926 *_results = results;
cd9adb8b 4927 results = nullptr;
588c4b0d
JG
4928 ret_code = LTTNG_OK;
4929end:
4930 lttng_trigger_put(matching_trigger);
4931 lttng_error_query_results_destroy(results);
4932 return ret_code;
4933}
4934
6dc3064a
DG
4935/*
4936 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4937 * snapshot output is *not* set with a remote destination.
4938 *
9a654598 4939 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 4940 */
28ab034a
JG
4941static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output,
4942 const struct ltt_session *session)
6dc3064a 4943{
9a654598 4944 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
4945 struct lttng_ht_iter iter;
4946 struct consumer_socket *socket;
1e791a74 4947 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
6fa5fe7c 4948 const char *base_path;
6dc3064a 4949
a0377dfe
FD
4950 LTTNG_ASSERT(output);
4951 LTTNG_ASSERT(session);
6dc3064a
DG
4952
4953 DBG2("Set relayd object from snapshot output");
4954
1e791a74 4955 if (session->current_trace_chunk) {
28ab034a
JG
4956 enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
4957 session->current_trace_chunk, &current_chunk_id.value);
1e791a74 4958
348a81dc 4959 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1e791a74
JG
4960 current_chunk_id.is_set = true;
4961 } else {
4962 ERR("Failed to get current trace chunk id");
4963 status = LTTNG_ERR_UNK;
4964 goto error;
4965 }
4966 }
4967
6dc3064a 4968 /* Ignore if snapshot consumer output is not network. */
348a81dc 4969 if (output->type != CONSUMER_DST_NET) {
6dc3064a
DG
4970 goto error;
4971 }
4972
6fa5fe7c
MD
4973 /*
4974 * The snapshot record URI base path overrides the session
4975 * base path.
4976 */
4977 if (output->dst.net.control.subdir[0] != '\0') {
4978 base_path = output->dst.net.control.subdir;
4979 } else {
4980 base_path = session->base_path;
4981 }
4982
6dc3064a
DG
4983 /*
4984 * For each consumer socket, create and send the relayd object of the
4985 * snapshot output.
4986 */
56047f5a
JG
4987 {
4988 lttng::urcu::read_lock_guard read_lock;
4989
4990 cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
4991 pthread_mutex_lock(socket->lock);
4992 status = send_consumer_relayd_sockets(
4993 session->id,
4994 output,
4995 socket,
4996 session->name,
4997 session->hostname,
4998 base_path,
4999 session->live_timer,
5000 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
5001 session->creation_time,
5002 session->name_contains_creation_time);
5003 pthread_mutex_unlock(socket->lock);
5004 if (status != LTTNG_OK) {
5005 goto error;
5006 }
6dc3064a
DG
5007 }
5008 }
6dc3064a
DG
5009
5010error:
9a654598 5011 return status;
6dc3064a
DG
5012}
5013
5014/*
5015 * Record a kernel snapshot.
5016 *
fac41e72 5017 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 5018 */
28ab034a
JG
5019static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
5020 const struct consumer_output *output,
5021 const struct ltt_session *session,
5022 uint64_t nb_packets_per_stream)
6dc3064a 5023{
9a654598 5024 enum lttng_error_code status;
6dc3064a 5025
a0377dfe
FD
5026 LTTNG_ASSERT(ksess);
5027 LTTNG_ASSERT(output);
5028 LTTNG_ASSERT(session);
6dc3064a 5029
28ab034a 5030 status = kernel_snapshot_record(ksess, output, nb_packets_per_stream);
9a654598 5031 return status;
6dc3064a
DG
5032}
5033
5034/*
5035 * Record a UST snapshot.
5036 *
9a654598 5037 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 5038 */
9a654598 5039static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
28ab034a
JG
5040 const struct consumer_output *output,
5041 const struct ltt_session *session,
5042 uint64_t nb_packets_per_stream)
6dc3064a 5043{
9a654598 5044 enum lttng_error_code status;
6dc3064a 5045
a0377dfe
FD
5046 LTTNG_ASSERT(usess);
5047 LTTNG_ASSERT(output);
5048 LTTNG_ASSERT(session);
6dc3064a 5049
28ab034a 5050 status = ust_app_snapshot_record(usess, output, nb_packets_per_stream);
9a654598 5051 return status;
6dc3064a
DG
5052}
5053
28ab034a
JG
5054static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session,
5055 uint64_t cur_nr_packets)
68808f4e 5056{
d07ceecd 5057 uint64_t tot_size = 0;
68808f4e
DG
5058
5059 if (session->kernel_session) {
5060 struct ltt_kernel_channel *chan;
28ab034a 5061 const struct ltt_kernel_session *ksess = session->kernel_session;
68808f4e 5062
28ab034a 5063 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
d07ceecd
MD
5064 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5065 /*
5066 * Don't take channel into account if we
5067 * already grab all its packets.
5068 */
5069 continue;
68808f4e 5070 }
28ab034a 5071 tot_size += chan->channel->attr.subbuf_size * chan->stream_count;
68808f4e
DG
5072 }
5073 }
5074
5075 if (session->ust_session) {
fb9a95c4 5076 const struct ltt_ust_session *usess = session->ust_session;
68808f4e 5077
28ab034a 5078 tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets);
68808f4e
DG
5079 }
5080
d07ceecd 5081 return tot_size;
68808f4e
DG
5082}
5083
5c786ded 5084/*
d07ceecd
MD
5085 * Calculate the number of packets we can grab from each stream that
5086 * fits within the overall snapshot max size.
5087 *
5088 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5089 * the number of packets per stream.
5090 *
5091 * TODO: this approach is not perfect: we consider the worse case
5092 * (packet filling the sub-buffers) as an upper bound, but we could do
5093 * better if we do this calculation while we actually grab the packet
5094 * content: we would know how much padding we don't actually store into
5095 * the file.
5096 *
5097 * This algorithm is currently bounded by the number of packets per
5098 * stream.
5099 *
5100 * Since we call this algorithm before actually grabbing the data, it's
5101 * an approximation: for instance, applications could appear/disappear
5102 * in between this call and actually grabbing data.
5c786ded 5103 */
28ab034a
JG
5104static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5105 uint64_t max_size)
5c786ded 5106{
d07ceecd
MD
5107 int64_t size_left;
5108 uint64_t cur_nb_packets = 0;
5c786ded 5109
d07ceecd 5110 if (!max_size) {
28ab034a 5111 return 0; /* Infinite */
5c786ded
JD
5112 }
5113
d07ceecd
MD
5114 size_left = max_size;
5115 for (;;) {
5116 uint64_t one_more_packet_tot_size;
5c786ded 5117
28ab034a
JG
5118 one_more_packet_tot_size =
5119 get_session_size_one_more_packet_per_stream(session, cur_nb_packets);
d07ceecd
MD
5120 if (!one_more_packet_tot_size) {
5121 /* We are already grabbing all packets. */
5122 break;
5123 }
5124 size_left -= one_more_packet_tot_size;
5125 if (size_left < 0) {
5126 break;
5127 }
5128 cur_nb_packets++;
5c786ded 5129 }
aecf2da5 5130 if (!cur_nb_packets && size_left != max_size) {
d07ceecd
MD
5131 /* Not enough room to grab one packet of each stream, error. */
5132 return -1;
5133 }
5134 return cur_nb_packets;
5c786ded
JD
5135}
5136
28ab034a
JG
5137static enum lttng_error_code snapshot_record(struct ltt_session *session,
5138 const struct snapshot_output *snapshot_output)
fb9a95c4
JG
5139{
5140 int64_t nb_packets_per_stream;
d2956687 5141 char snapshot_chunk_name[LTTNG_NAME_MAX];
348a81dc
JG
5142 int ret;
5143 enum lttng_error_code ret_code = LTTNG_OK;
d2956687 5144 struct lttng_trace_chunk *snapshot_trace_chunk;
cd9adb8b
JG
5145 struct consumer_output *original_ust_consumer_output = nullptr;
5146 struct consumer_output *original_kernel_consumer_output = nullptr;
5147 struct consumer_output *snapshot_ust_consumer_output = nullptr;
5148 struct consumer_output *snapshot_kernel_consumer_output = nullptr;
d2956687 5149
28ab034a
JG
5150 ret = snprintf(snapshot_chunk_name,
5151 sizeof(snapshot_chunk_name),
5152 "%s-%s-%" PRIu64,
5153 snapshot_output->name,
5154 snapshot_output->datetime,
5155 snapshot_output->nb_snapshot);
348a81dc 5156 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
d2956687 5157 ERR("Failed to format snapshot name");
348a81dc
JG
5158 ret_code = LTTNG_ERR_INVALID;
5159 goto error;
d2956687
JG
5160 }
5161 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
28ab034a
JG
5162 snapshot_output->name,
5163 session->name,
5164 snapshot_chunk_name);
348a81dc
JG
5165 if (!session->kernel_session && !session->ust_session) {
5166 ERR("Failed to record snapshot as no channels exist");
5167 ret_code = LTTNG_ERR_NO_CHANNEL;
5168 goto error;
5169 }
5170
5171 if (session->kernel_session) {
28ab034a
JG
5172 original_kernel_consumer_output = session->kernel_session->consumer;
5173 snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer);
5174 strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name);
bd666153
JR
5175
5176 /* Copy the original domain subdir. */
5177 strcpy(snapshot_kernel_consumer_output->domain_subdir,
28ab034a 5178 original_kernel_consumer_output->domain_subdir);
bd666153 5179
348a81dc 5180 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
28ab034a 5181 original_kernel_consumer_output);
348a81dc
JG
5182 if (ret < 0) {
5183 ERR("Failed to copy consumer sockets from snapshot output configuration");
5184 ret_code = LTTNG_ERR_NOMEM;
5185 goto error;
5186 }
28ab034a 5187 ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session);
348a81dc
JG
5188 if (ret_code != LTTNG_OK) {
5189 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5190 goto error;
5191 }
28ab034a 5192 session->kernel_session->consumer = snapshot_kernel_consumer_output;
348a81dc
JG
5193 }
5194 if (session->ust_session) {
5195 original_ust_consumer_output = session->ust_session->consumer;
28ab034a
JG
5196 snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer);
5197 strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name);
bd666153
JR
5198
5199 /* Copy the original domain subdir. */
5200 strcpy(snapshot_ust_consumer_output->domain_subdir,
28ab034a 5201 original_ust_consumer_output->domain_subdir);
bd666153 5202
348a81dc 5203 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
28ab034a 5204 original_ust_consumer_output);
348a81dc
JG
5205 if (ret < 0) {
5206 ERR("Failed to copy consumer sockets from snapshot output configuration");
5207 ret_code = LTTNG_ERR_NOMEM;
5208 goto error;
5209 }
28ab034a 5210 ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session);
348a81dc
JG
5211 if (ret_code != LTTNG_OK) {
5212 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5213 goto error;
5214 }
28ab034a 5215 session->ust_session->consumer = snapshot_ust_consumer_output;
348a81dc
JG
5216 }
5217
28ab034a
JG
5218 snapshot_trace_chunk = session_create_new_trace_chunk(
5219 session,
5220 snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output,
5221 consumer_output_get_base_path(snapshot_output->consumer),
5222 snapshot_chunk_name);
d2956687 5223 if (!snapshot_trace_chunk) {
348a81dc 5224 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
28ab034a 5225 session->name);
348a81dc
JG
5226 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5227 goto error;
d2956687 5228 }
a0377dfe 5229 LTTNG_ASSERT(!session->current_trace_chunk);
cd9adb8b 5230 ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr);
d2956687 5231 lttng_trace_chunk_put(snapshot_trace_chunk);
cd9adb8b 5232 snapshot_trace_chunk = nullptr;
d2956687 5233 if (ret) {
348a81dc 5234 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
28ab034a 5235 session->name);
348a81dc
JG
5236 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5237 goto error;
d2956687 5238 }
fb9a95c4 5239
28ab034a
JG
5240 nb_packets_per_stream =
5241 get_session_nb_packets_per_stream(session, snapshot_output->max_size);
fb9a95c4 5242 if (nb_packets_per_stream < 0) {
348a81dc 5243 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5151d412 5244 goto error_close_trace_chunk;
fb9a95c4
JG
5245 }
5246
5247 if (session->kernel_session) {
348a81dc 5248 ret_code = record_kernel_snapshot(session->kernel_session,
28ab034a
JG
5249 snapshot_kernel_consumer_output,
5250 session,
5251 nb_packets_per_stream);
348a81dc 5252 if (ret_code != LTTNG_OK) {
5151d412 5253 goto error_close_trace_chunk;
fb9a95c4
JG
5254 }
5255 }
5256
5257 if (session->ust_session) {
348a81dc 5258 ret_code = record_ust_snapshot(session->ust_session,
28ab034a
JG
5259 snapshot_ust_consumer_output,
5260 session,
5261 nb_packets_per_stream);
348a81dc 5262 if (ret_code != LTTNG_OK) {
5151d412 5263 goto error_close_trace_chunk;
fb9a95c4
JG
5264 }
5265 }
d2956687 5266
5151d412 5267error_close_trace_chunk:
cd9adb8b 5268 if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) {
28ab034a 5269 ERR("Failed to release the current trace chunk of session \"%s\"", session->name);
dbfee52c
MD
5270 ret_code = LTTNG_ERR_UNK;
5271 }
5272
28ab034a
JG
5273 if (session_close_trace_chunk(session,
5274 snapshot_trace_chunk,
5275 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION,
cd9adb8b 5276 nullptr)) {
d2956687
JG
5277 /*
5278 * Don't goto end; make sure the chunk is closed for the session
5279 * to allow future snapshots.
5280 */
28ab034a 5281 ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name);
348a81dc 5282 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
d2956687 5283 }
a49a9216
JG
5284
5285 lttng_trace_chunk_put(snapshot_trace_chunk);
cd9adb8b 5286 snapshot_trace_chunk = nullptr;
348a81dc
JG
5287error:
5288 if (original_ust_consumer_output) {
5289 session->ust_session->consumer = original_ust_consumer_output;
5290 }
5291 if (original_kernel_consumer_output) {
28ab034a 5292 session->kernel_session->consumer = original_kernel_consumer_output;
348a81dc
JG
5293 }
5294 consumer_output_put(snapshot_ust_consumer_output);
5295 consumer_output_put(snapshot_kernel_consumer_output);
5296 return ret_code;
fb9a95c4
JG
5297}
5298
6dc3064a
DG
5299/*
5300 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5301 *
5302 * The wait parameter is ignored so this call always wait for the snapshot to
5303 * complete before returning.
5304 *
5305 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5306 */
5307int cmd_snapshot_record(struct ltt_session *session,
28ab034a
JG
5308 const struct lttng_snapshot_output *output,
5309 int wait __attribute__((unused)))
6dc3064a 5310{
9a654598
JG
5311 enum lttng_error_code cmd_ret = LTTNG_OK;
5312 int ret;
00e1dfc4 5313 unsigned int snapshot_success = 0;
10ba83fe 5314 char datetime[16];
cd9adb8b 5315 struct snapshot_output *tmp_output = nullptr;
6dc3064a 5316
a0377dfe
FD
5317 LTTNG_ASSERT(session);
5318 LTTNG_ASSERT(output);
6dc3064a
DG
5319
5320 DBG("Cmd snapshot record for session %s", session->name);
5321
10ba83fe 5322 /* Get the datetime for the snapshot output directory. */
28ab034a 5323 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime));
10ba83fe 5324 if (!ret) {
9a654598 5325 cmd_ret = LTTNG_ERR_INVALID;
10ba83fe
JR
5326 goto error;
5327 }
5328
6dc3064a 5329 /*
d3f14b8a
MD
5330 * Permission denied to create an output if the session is not
5331 * set in no output mode.
6dc3064a
DG
5332 */
5333 if (session->output_traces) {
9a654598 5334 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
5335 goto error;
5336 }
5337
5338 /* The session needs to be started at least once. */
8382cf6f 5339 if (!session->has_been_started) {
9a654598 5340 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
6dc3064a
DG
5341 goto error;
5342 }
5343
5344 /* Use temporary output for the session. */
ba45d9f0 5345 if (*output->ctrl_url != '\0') {
2abe7969
JG
5346 tmp_output = snapshot_output_alloc();
5347 if (!tmp_output) {
5348 cmd_ret = LTTNG_ERR_NOMEM;
5349 goto error;
5350 }
5351
28ab034a
JG
5352 ret = snapshot_output_init(session,
5353 output->max_size,
5354 output->name,
5355 output->ctrl_url,
5356 output->data_url,
5357 session->consumer,
5358 tmp_output,
cd9adb8b 5359 nullptr);
6dc3064a
DG
5360 if (ret < 0) {
5361 if (ret == -ENOMEM) {
9a654598 5362 cmd_ret = LTTNG_ERR_NOMEM;
6dc3064a 5363 } else {
9a654598 5364 cmd_ret = LTTNG_ERR_INVALID;
6dc3064a
DG
5365 }
5366 goto error;
5367 }
1bfe7328 5368 /* Use the global session count for the temporary snapshot. */
2abe7969 5369 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
10ba83fe
JR
5370
5371 /* Use the global datetime */
2abe7969 5372 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
f46376a1 5373 cmd_ret = snapshot_record(session, tmp_output);
fb9a95c4 5374 if (cmd_ret != LTTNG_OK) {
804c90a8
JR
5375 goto error;
5376 }
804c90a8
JR
5377 snapshot_success = 1;
5378 } else {
5379 struct snapshot_output *sout;
5380 struct lttng_ht_iter iter;
68808f4e 5381
56047f5a
JG
5382 lttng::urcu::read_lock_guard read_lock;
5383
28ab034a
JG
5384 cds_lfht_for_each_entry (
5385 session->snapshot.output_ht->ht, &iter.iter, sout, node.node) {
2abe7969
JG
5386 struct snapshot_output output_copy;
5387
804c90a8 5388 /*
2abe7969
JG
5389 * Make a local copy of the output and override output
5390 * parameters with those provided as part of the
5391 * command.
804c90a8 5392 */
2abe7969 5393 memcpy(&output_copy, sout, sizeof(output_copy));
1bfe7328 5394
804c90a8 5395 if (output->max_size != (uint64_t) -1ULL) {
2abe7969 5396 output_copy.max_size = output->max_size;
6dc3064a 5397 }
d07ceecd 5398
2abe7969 5399 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
28ab034a 5400 memcpy(output_copy.datetime, datetime, sizeof(datetime));
6dc3064a 5401
804c90a8
JR
5402 /* Use temporary name. */
5403 if (*output->name != '\0') {
2abe7969 5404 if (lttng_strncpy(output_copy.name,
28ab034a
JG
5405 output->name,
5406 sizeof(output_copy.name))) {
9a654598 5407 cmd_ret = LTTNG_ERR_INVALID;
cf3e357d
MD
5408 goto error;
5409 }
804c90a8 5410 }
e1986656 5411
f46376a1 5412 cmd_ret = snapshot_record(session, &output_copy);
fb9a95c4 5413 if (cmd_ret != LTTNG_OK) {
fb9a95c4 5414 goto error;
6dc3064a 5415 }
56047f5a 5416
804c90a8 5417 snapshot_success = 1;
6dc3064a
DG
5418 }
5419 }
5420
1bfe7328
DG
5421 if (snapshot_success) {
5422 session->snapshot.nb_snapshot++;
b67578cb 5423 } else {
9a654598 5424 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
5425 }
5426
6dc3064a 5427error:
2abe7969
JG
5428 if (tmp_output) {
5429 snapshot_output_destroy(tmp_output);
5430 }
56047f5a 5431
9a654598 5432 return cmd_ret;
6dc3064a
DG
5433}
5434
d7ba1388
MD
5435/*
5436 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5437 */
28ab034a 5438int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
d7ba1388
MD
5439{
5440 /* Safety net */
a0377dfe 5441 LTTNG_ASSERT(session);
d7ba1388
MD
5442
5443 /*
5444 * Can only set shm path before session is started.
5445 */
5446 if (session->has_been_started) {
5447 return LTTNG_ERR_SESSION_STARTED;
5448 }
5449
28ab034a 5450 strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
d7ba1388
MD
5451 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5452
7e397c55 5453 return LTTNG_OK;
d7ba1388
MD
5454}
5455
5c408ad8
JD
5456/*
5457 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5458 *
5459 * Ask the consumer to rotate the session output directory.
5460 * The session lock must be held.
5461 *
d5a1b7aa 5462 * Returns LTTNG_OK on success or else a negative LTTng error code.
5c408ad8
JD
5463 */
5464int cmd_rotate_session(struct ltt_session *session,
28ab034a
JG
5465 struct lttng_rotate_session_return *rotate_return,
5466 bool quiet_rotation,
5467 enum lttng_trace_chunk_command_type command)
5c408ad8
JD
5468{
5469 int ret;
d2956687 5470 uint64_t ongoing_rotation_chunk_id;
d5a1b7aa 5471 enum lttng_error_code cmd_ret = LTTNG_OK;
cd9adb8b
JG
5472 struct lttng_trace_chunk *chunk_being_archived = nullptr;
5473 struct lttng_trace_chunk *new_trace_chunk = nullptr;
d2956687 5474 enum lttng_trace_chunk_status chunk_status;
3156892b
JG
5475 bool failed_to_rotate = false;
5476 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5c408ad8 5477
a0377dfe 5478 LTTNG_ASSERT(session);
5c408ad8
JD
5479
5480 if (!session->has_been_started) {
d5a1b7aa 5481 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
d68c9a04 5482 goto end;
5c408ad8
JD
5483 }
5484
d48d65e1
MD
5485 /*
5486 * Explicit rotation is not supported for live sessions.
5487 * However, live sessions can perform a quiet rotation on
5488 * destroy.
5489 * Rotation is not supported for snapshot traces (no output).
5490 */
28ab034a 5491 if ((!quiet_rotation && session->live_timer) || !session->output_traces) {
d5a1b7aa 5492 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
d68c9a04 5493 goto end;
5c408ad8
JD
5494 }
5495
d2956687 5496 /* Unsupported feature in lttng-relayd before 2.11. */
070b6a86 5497 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
28ab034a
JG
5498 (session->consumer->relay_major_version == 2 &&
5499 session->consumer->relay_minor_version < 11)) {
d5a1b7aa 5500 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
d68c9a04 5501 goto end;
5c408ad8
JD
5502 }
5503
a40a503f
MD
5504 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5505 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5506 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5507 goto end;
5508 }
5509
92816cc3 5510 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
92816cc3 5511 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
28ab034a 5512 session->name);
d5a1b7aa 5513 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
d68c9a04 5514 goto end;
5c408ad8
JD
5515 }
5516
5517 /*
5518 * After a stop, we only allow one rotation to occur, the other ones are
5519 * useless until a new start.
5520 */
5521 if (session->rotated_after_last_stop) {
5522 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
28ab034a 5523 session->name);
d5a1b7aa 5524 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
d68c9a04 5525 goto end;
5c408ad8 5526 }
b02f5986
MD
5527
5528 /*
5529 * After a stop followed by a clear, disallow following rotations a they would
5530 * generate empty chunks.
5531 */
5532 if (session->cleared_after_last_stop) {
5533 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
28ab034a 5534 session->name);
b02f5986
MD
5535 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5536 goto end;
5537 }
5538
d2956687 5539 if (session->active) {
cd9adb8b
JG
5540 new_trace_chunk =
5541 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
d2956687
JG
5542 if (!new_trace_chunk) {
5543 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5544 goto error;
5c408ad8 5545 }
0e270a1e 5546 }
2961f09e 5547
3156892b
JG
5548 /*
5549 * The current trace chunk becomes the chunk being archived.
5550 *
5551 * After this point, "chunk_being_archived" must absolutely
5552 * be closed on the consumer(s), otherwise it will never be
5553 * cleaned-up, which will result in a leak.
5554 */
28ab034a 5555 ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived);
d2956687
JG
5556 if (ret) {
5557 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
b178f53e
JG
5558 goto error;
5559 }
5560
5c408ad8 5561 if (session->kernel_session) {
d5a1b7aa
JG
5562 cmd_ret = kernel_rotate_session(session);
5563 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5564 failed_to_rotate = true;
5565 rotation_fail_code = cmd_ret;
5c408ad8
JD
5566 }
5567 }
5568 if (session->ust_session) {
d5a1b7aa
JG
5569 cmd_ret = ust_app_rotate_session(session);
5570 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5571 failed_to_rotate = true;
5572 rotation_fail_code = cmd_ret;
5c408ad8 5573 }
92816cc3 5574 }
17dd1232 5575
3b61d9ee
JG
5576 if (!session->active) {
5577 session->rotated_after_last_stop = true;
5578 }
5579
5580 if (!chunk_being_archived) {
5581 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
28ab034a 5582 session->name);
3b61d9ee
JG
5583 if (failed_to_rotate) {
5584 cmd_ret = rotation_fail_code;
5585 goto error;
5586 }
5587 cmd_ret = LTTNG_OK;
5588 goto end;
5589 }
5590
5591 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
28ab034a 5592 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id);
a0377dfe 5593 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
3b61d9ee 5594
28ab034a
JG
5595 ret = session_close_trace_chunk(
5596 session, chunk_being_archived, command, session->last_chunk_path);
d2956687
JG
5597 if (ret) {
5598 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5599 goto error;
5600 }
5601
3156892b
JG
5602 if (failed_to_rotate) {
5603 cmd_ret = rotation_fail_code;
5604 goto error;
5605 }
5606
7fdbed1c 5607 session->quiet_rotation = quiet_rotation;
28ab034a 5608 ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER);
92816cc3 5609 if (ret) {
d5a1b7aa 5610 cmd_ret = LTTNG_ERR_UNK;
2961f09e 5611 goto error;
5c408ad8
JD
5612 }
5613
5c408ad8 5614 if (rotate_return) {
d2956687 5615 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5c408ad8
JD
5616 }
5617
d2956687 5618 session->chunk_being_archived = chunk_being_archived;
cd9adb8b 5619 chunk_being_archived = nullptr;
7fdbed1c
JG
5620 if (!quiet_rotation) {
5621 ret = notification_thread_command_session_rotation_ongoing(
28ab034a 5622 the_notification_thread_handle, session->id, ongoing_rotation_chunk_id);
7fdbed1c
JG
5623 if (ret != LTTNG_OK) {
5624 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
28ab034a 5625 session->name);
7966af57 5626 cmd_ret = (lttng_error_code) ret;
7fdbed1c 5627 }
2961f09e
JG
5628 }
5629
92816cc3 5630 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
28ab034a
JG
5631 session->name,
5632 ongoing_rotation_chunk_id);
5c408ad8 5633end:
d2956687
JG
5634 lttng_trace_chunk_put(new_trace_chunk);
5635 lttng_trace_chunk_put(chunk_being_archived);
d5a1b7aa 5636 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5c408ad8 5637 return ret;
2961f09e 5638error:
0038180d 5639 if (session_reset_rotation_state(*session, LTTNG_ROTATION_STATE_ERROR)) {
28ab034a 5640 ERR("Failed to reset rotation state of session \"%s\"", session->name);
2961f09e
JG
5641 }
5642 goto end;
5c408ad8
JD
5643}
5644
5645/*
d68c9a04 5646 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5c408ad8
JD
5647 *
5648 * Check if the session has finished its rotation.
5649 *
d2956687 5650 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5c408ad8 5651 */
d68c9a04 5652int cmd_rotate_get_info(struct ltt_session *session,
28ab034a
JG
5653 struct lttng_rotation_get_info_return *info_return,
5654 uint64_t rotation_id)
5c408ad8 5655{
d2956687
JG
5656 enum lttng_error_code cmd_ret = LTTNG_OK;
5657 enum lttng_rotation_state rotation_state;
5c408ad8 5658
28ab034a
JG
5659 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64,
5660 session->name,
5661 session->most_recent_chunk_id.value);
5c408ad8 5662
d2956687
JG
5663 if (session->chunk_being_archived) {
5664 enum lttng_trace_chunk_status chunk_status;
5665 uint64_t chunk_id;
5666
28ab034a 5667 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
a0377dfe 5668 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
d2956687 5669
28ab034a
JG
5670 rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING :
5671 LTTNG_ROTATION_STATE_EXPIRED;
d2956687
JG
5672 } else {
5673 if (session->last_archived_chunk_id.is_set &&
28ab034a 5674 rotation_id != session->last_archived_chunk_id.value) {
d2956687
JG
5675 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5676 } else {
5677 rotation_state = session->rotation_state;
5678 }
5c408ad8
JD
5679 }
5680
d2956687
JG
5681 switch (rotation_state) {
5682 case LTTNG_ROTATION_STATE_NO_ROTATION:
83ed9e90 5683 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
28ab034a 5684 session->name);
d2956687
JG
5685 goto end;
5686 case LTTNG_ROTATION_STATE_EXPIRED:
28ab034a
JG
5687 DBG("Reporting that the rotation state of rotation id %" PRIu64
5688 " of session \"%s\" has expired",
5689 rotation_id,
5690 session->name);
d2956687 5691 break;
d68c9a04 5692 case LTTNG_ROTATION_STATE_ONGOING:
d2956687 5693 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
28ab034a
JG
5694 rotation_id,
5695 session->name);
d68c9a04
JD
5696 break;
5697 case LTTNG_ROTATION_STATE_COMPLETED:
dd73d57b 5698 {
d2956687
JG
5699 int fmt_ret;
5700 char *chunk_path;
dd73d57b
JG
5701 char *current_tracing_path_reply;
5702 size_t current_tracing_path_reply_len;
5703
d2956687 5704 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
28ab034a
JG
5705 rotation_id,
5706 session->name);
d2956687 5707
dd73d57b
JG
5708 switch (session_get_consumer_destination_type(session)) {
5709 case CONSUMER_DST_LOCAL:
28ab034a 5710 current_tracing_path_reply = info_return->location.local.absolute_path;
dd73d57b 5711 current_tracing_path_reply_len =
28ab034a 5712 sizeof(info_return->location.local.absolute_path);
dd73d57b 5713 info_return->location_type =
28ab034a 5714 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
ecd1a12f 5715 fmt_ret = asprintf(&chunk_path,
28ab034a
JG
5716 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5717 session_get_base_path(session),
5718 session->last_archived_chunk_name);
ecd1a12f
MD
5719 if (fmt_ret == -1) {
5720 PERROR("Failed to format the path of the last archived trace chunk");
5721 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5722 cmd_ret = LTTNG_ERR_UNK;
5723 goto end;
5724 }
dd73d57b
JG
5725 break;
5726 case CONSUMER_DST_NET:
09cfbe47
JG
5727 {
5728 uint16_t ctrl_port, data_port;
5729
28ab034a 5730 current_tracing_path_reply = info_return->location.relay.relative_path;
dd73d57b 5731 current_tracing_path_reply_len =
28ab034a 5732 sizeof(info_return->location.relay.relative_path);
dd73d57b
JG
5733 /* Currently the only supported relay protocol. */
5734 info_return->location.relay.protocol =
28ab034a 5735 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
dd73d57b 5736
d2956687 5737 fmt_ret = lttng_strncpy(info_return->location.relay.host,
28ab034a
JG
5738 session_get_net_consumer_hostname(session),
5739 sizeof(info_return->location.relay.host));
d2956687
JG
5740 if (fmt_ret) {
5741 ERR("Failed to copy host name to rotate_get_info reply");
dd73d57b 5742 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5743 cmd_ret = LTTNG_ERR_SET_URL;
dd73d57b
JG
5744 goto end;
5745 }
5746
09cfbe47
JG
5747 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5748 info_return->location.relay.ports.control = ctrl_port;
5749 info_return->location.relay.ports.data = data_port;
dd73d57b 5750 info_return->location_type =
28ab034a 5751 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
ecd1a12f
MD
5752 chunk_path = strdup(session->last_chunk_path);
5753 if (!chunk_path) {
5754 ERR("Failed to allocate the path of the last archived trace chunk");
5755 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5756 cmd_ret = LTTNG_ERR_UNK;
5757 goto end;
5758 }
dd73d57b 5759 break;
09cfbe47 5760 }
dd73d57b
JG
5761 default:
5762 abort();
5763 }
d2956687 5764
28ab034a
JG
5765 fmt_ret = lttng_strncpy(
5766 current_tracing_path_reply, chunk_path, current_tracing_path_reply_len);
d2956687
JG
5767 free(chunk_path);
5768 if (fmt_ret) {
5769 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
d68c9a04 5770 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5771 cmd_ret = LTTNG_ERR_UNK;
5c408ad8
JD
5772 goto end;
5773 }
dd73d57b 5774
d68c9a04 5775 break;
dd73d57b 5776 }
d68c9a04 5777 case LTTNG_ROTATION_STATE_ERROR:
28ab034a
JG
5778 DBG("Reporting that an error occurred during rotation %" PRIu64
5779 " of session \"%s\"",
5780 rotation_id,
5781 session->name);
d68c9a04
JD
5782 break;
5783 default:
5784 abort();
5c408ad8
JD
5785 }
5786
d2956687 5787 cmd_ret = LTTNG_OK;
5c408ad8 5788end:
d2956687
JG
5789 info_return->status = (int32_t) rotation_state;
5790 return cmd_ret;
5c408ad8
JD
5791}
5792
259c2674
JD
5793/*
5794 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5795 *
5796 * Configure the automatic rotation parameters.
66ea93b1
JG
5797 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5798 * 'activate' to false means deactivate the rotation schedule and validate that
5799 * 'new_value' has the same value as the currently active value.
259c2674 5800 *
1136f41b 5801 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
259c2674
JD
5802 */
5803int cmd_rotation_set_schedule(struct ltt_session *session,
28ab034a
JG
5804 bool activate,
5805 enum lttng_rotation_schedule_type schedule_type,
0038180d 5806 uint64_t new_value)
259c2674
JD
5807{
5808 int ret;
66ea93b1 5809 uint64_t *parameter_value;
259c2674 5810
a0377dfe 5811 LTTNG_ASSERT(session);
259c2674
JD
5812
5813 DBG("Cmd rotate set schedule session %s", session->name);
5814
92fe5ca1 5815 if (session->live_timer || !session->output_traces) {
66ea93b1 5816 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
259c2674
JD
5817 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5818 goto end;
5819 }
5820
66ea93b1
JG
5821 switch (schedule_type) {
5822 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5823 parameter_value = &session->rotate_size;
5824 break;
5825 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5826 parameter_value = &session->rotate_timer_period;
5827 if (new_value >= UINT_MAX) {
28ab034a
JG
5828 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5829 " > %u (UINT_MAX)",
5830 new_value,
5831 UINT_MAX);
66ea93b1
JG
5832 ret = LTTNG_ERR_INVALID;
5833 goto end;
5834 }
5835 break;
5836 default:
5837 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5838 ret = LTTNG_ERR_INVALID;
259c2674 5839 goto end;
66ea93b1
JG
5840 }
5841
5842 /* Improper use of the API. */
5843 if (new_value == -1ULL) {
5844 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5845 ret = LTTNG_ERR_INVALID;
259c2674
JD
5846 goto end;
5847 }
5848
66ea93b1
JG
5849 /*
5850 * As indicated in struct ltt_session's comments, a value of == 0 means
5851 * this schedule rotation type is not in use.
5852 *
5853 * Reject the command if we were asked to activate a schedule that was
5854 * already active.
5855 */
5856 if (activate && *parameter_value != 0) {
5857 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5858 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
90936dcf 5859 goto end;
66ea93b1
JG
5860 }
5861
5862 /*
5863 * Reject the command if we were asked to deactivate a schedule that was
5864 * not active.
5865 */
5866 if (!activate && *parameter_value == 0) {
5867 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5868 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
90936dcf
JD
5869 goto end;
5870 }
5871
66ea93b1
JG
5872 /*
5873 * Reject the command if we were asked to deactivate a schedule that
5874 * doesn't exist.
5875 */
5876 if (!activate && *parameter_value != new_value) {
5877 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5878 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5879 goto end;
5880 }
259c2674 5881
66ea93b1
JG
5882 *parameter_value = activate ? new_value : 0;
5883
5884 switch (schedule_type) {
5885 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5886 if (activate && session->active) {
5887 /*
5888 * Only start the timer if the session is active,
5889 * otherwise it will be started when the session starts.
5890 */
28ab034a 5891 ret = timer_session_rotation_schedule_timer_start(session, new_value);
259c2674 5892 if (ret) {
66ea93b1 5893 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
259c2674
JD
5894 ret = LTTNG_ERR_UNK;
5895 goto end;
5896 }
66ea93b1 5897 } else {
28ab034a 5898 ret = timer_session_rotation_schedule_timer_stop(session);
66ea93b1
JG
5899 if (ret) {
5900 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5901 ret = LTTNG_ERR_UNK;
f3ce6946 5902 goto end;
66ea93b1 5903 }
259c2674 5904 }
66ea93b1
JG
5905 break;
5906 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5907 if (activate) {
0038180d
JG
5908 try {
5909 the_rotation_thread_handle->subscribe_session_consumed_size_rotation(
5910 *session, new_value);
5b9eda8a 5911 } catch (const std::exception& e) {
0038180d
JG
5912 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5913 e.what());
90936dcf
JD
5914 ret = LTTNG_ERR_UNK;
5915 goto end;
5916 }
90936dcf 5917 } else {
0038180d
JG
5918 try {
5919 the_rotation_thread_handle
5920 ->unsubscribe_session_consumed_size_rotation(*session);
5b9eda8a 5921 } catch (const std::exception& e) {
0038180d
JG
5922 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5923 e.what());
90936dcf
JD
5924 ret = LTTNG_ERR_UNK;
5925 goto end;
5926 }
90936dcf 5927 }
66ea93b1
JG
5928 break;
5929 default:
5930 /* Would have been caught before. */
5931 abort();
90936dcf
JD
5932 }
5933
259c2674
JD
5934 ret = LTTNG_OK;
5935
5936 goto end;
5937
5938end:
5939 return ret;
5940}
5941
a503e1ef
JG
5942/* Wait for a given path to be removed before continuing. */
5943static enum lttng_error_code wait_on_path(void *path_data)
5944{
7966af57 5945 const char *shm_path = (const char *) path_data;
a503e1ef
JG
5946
5947 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
28ab034a 5948 shm_path);
a503e1ef
JG
5949 while (true) {
5950 int ret;
5951 struct stat st;
5952
5953 ret = stat(shm_path, &st);
5954 if (ret) {
5955 if (errno != ENOENT) {
5956 PERROR("stat() returned an error while checking for the existence of the shm path");
5957 } else {
5958 DBG("shm path no longer exists, completing the destruction of session");
5959 }
5960 break;
5961 } else {
5962 if (!S_ISDIR(st.st_mode)) {
5963 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
28ab034a 5964 shm_path);
a503e1ef
JG
5965 break;
5966 }
5967 }
5968 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5969 }
5970 return LTTNG_OK;
5971}
5972
5973/*
5974 * Returns a pointer to a handler to run on completion of a command.
5975 * Returns NULL if no handler has to be run for the last command executed.
5976 */
cd9adb8b 5977const struct cmd_completion_handler *cmd_pop_completion_handler()
a503e1ef
JG
5978{
5979 struct cmd_completion_handler *handler = current_completion_handler;
5980
cd9adb8b 5981 current_completion_handler = nullptr;
a503e1ef
JG
5982 return handler;
5983}
5984
2f77fc4b
DG
5985/*
5986 * Init command subsystem.
5987 */
cd9adb8b 5988void cmd_init()
2f77fc4b
DG
5989{
5990 /*
d88aee68
DG
5991 * Set network sequence index to 1 for streams to match a relayd
5992 * socket on the consumer side.
2f77fc4b 5993 */
d88aee68
DG
5994 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5995 relayd_net_seq_idx = 1;
5996 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
5997
5998 DBG("Command subsystem initialized");
5999}
This page took 0.5125 seconds and 4 git commands to generate.