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