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