Tests: Avoid looping on app PIDs during cleanup
[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
56047f5a 2098 lttng::urcu::read_lock_guard read_lock;
9f449915 2099
7972aab2 2100 switch (domain->type) {
2f77fc4b
DG
2101 case LTTNG_DOMAIN_KERNEL:
2102 {
2103 struct ltt_kernel_channel *kchan;
2104
85076754
MD
2105 /*
2106 * If a non-default channel has been created in the
2107 * session, explicitely require that -c chan_name needs
2108 * to be provided.
2109 */
28ab034a 2110 if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') {
85076754
MD
2111 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2112 goto error;
2113 }
2114
28ab034a 2115 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
cd9adb8b 2116 if (kchan == nullptr) {
28ab034a 2117 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL);
cd9adb8b 2118 if (attr == nullptr) {
f73fabfd 2119 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2120 goto error;
2121 }
28ab034a 2122 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
04c17253 2123 ret = LTTNG_ERR_INVALID;
04c17253
MD
2124 goto error;
2125 }
2f77fc4b 2126
28ab034a 2127 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
f73fabfd 2128 if (ret != LTTNG_OK) {
2f77fc4b
DG
2129 goto error;
2130 }
e5f5db7f 2131 channel_created = 1;
2f77fc4b
DG
2132 }
2133
2134 /* Get the newly created kernel channel pointer */
28ab034a 2135 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
cd9adb8b 2136 if (kchan == nullptr) {
2f77fc4b 2137 /* This sould not happen... */
f73fabfd 2138 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2139 goto error;
2140 }
2141
6e911cad
MD
2142 switch (event->type) {
2143 case LTTNG_EVENT_ALL:
29c62722 2144 {
cd9adb8b
JG
2145 char *filter_expression_a = nullptr;
2146 struct lttng_bytecode *filter_a = nullptr;
00a62084
MD
2147
2148 /*
2149 * We need to duplicate filter_expression and filter,
2150 * because ownership is passed to first enable
2151 * event.
2152 */
2153 if (filter_expression) {
2154 filter_expression_a = strdup(filter_expression);
2155 if (!filter_expression_a) {
2156 ret = LTTNG_ERR_FATAL;
2157 goto error;
2158 }
2159 }
2160 if (filter) {
64803277 2161 filter_a = zmalloc<lttng_bytecode>(sizeof(*filter_a) + filter->len);
00a62084
MD
2162 if (!filter_a) {
2163 free(filter_expression_a);
2164 ret = LTTNG_ERR_FATAL;
2165 goto error;
2166 }
2167 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2168 }
28ab034a
JG
2169 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2170 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
a969e101 2171 /* We have passed ownership */
cd9adb8b
JG
2172 filter_expression = nullptr;
2173 filter = nullptr;
29c62722
MD
2174 if (ret != LTTNG_OK) {
2175 if (channel_created) {
2176 /* Let's not leak a useless channel. */
2177 kernel_destroy_channel(kchan);
2178 }
00a62084
MD
2179 free(filter_expression_a);
2180 free(filter_a);
29c62722
MD
2181 goto error;
2182 }
28ab034a
JG
2183 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2184 ret = event_kernel_enable_event(
2185 kchan, event, filter_expression_a, filter_a);
60d21fa2 2186 /* We have passed ownership */
cd9adb8b
JG
2187 filter_expression_a = nullptr;
2188 filter_a = nullptr;
29c62722
MD
2189 if (ret != LTTNG_OK) {
2190 goto error;
2191 }
2192 break;
2193 }
6e6ef3d7 2194 case LTTNG_EVENT_PROBE:
dcabc190 2195 case LTTNG_EVENT_USERSPACE_PROBE:
6e6ef3d7
DG
2196 case LTTNG_EVENT_FUNCTION:
2197 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 2198 case LTTNG_EVENT_TRACEPOINT:
28ab034a 2199 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
a969e101 2200 /* We have passed ownership */
cd9adb8b
JG
2201 filter_expression = nullptr;
2202 filter = nullptr;
6e911cad
MD
2203 if (ret != LTTNG_OK) {
2204 if (channel_created) {
2205 /* Let's not leak a useless channel. */
2206 kernel_destroy_channel(kchan);
2207 }
2208 goto error;
e5f5db7f 2209 }
6e911cad
MD
2210 break;
2211 case LTTNG_EVENT_SYSCALL:
28ab034a 2212 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
a969e101 2213 /* We have passed ownership */
cd9adb8b
JG
2214 filter_expression = nullptr;
2215 filter = nullptr;
e2b957af
MD
2216 if (ret != LTTNG_OK) {
2217 goto error;
2218 }
6e911cad
MD
2219 break;
2220 default:
2221 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
2222 goto error;
2223 }
2224
7d268848 2225 kernel_wait_quiescent();
2f77fc4b
DG
2226 break;
2227 }
2228 case LTTNG_DOMAIN_UST:
2229 {
2230 struct ltt_ust_channel *uchan;
2231 struct ltt_ust_session *usess = session->ust_session;
2232
a0377dfe 2233 LTTNG_ASSERT(usess);
2f77fc4b 2234
85076754
MD
2235 /*
2236 * If a non-default channel has been created in the
2237 * session, explicitely require that -c chan_name needs
2238 * to be provided.
2239 */
2240 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2241 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2242 goto error;
2243 }
2244
2f77fc4b 2245 /* Get channel from global UST domain */
28ab034a 2246 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
cd9adb8b 2247 if (uchan == nullptr) {
2f77fc4b 2248 /* Create default channel */
28ab034a 2249 attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type);
cd9adb8b 2250 if (attr == nullptr) {
f73fabfd 2251 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2252 goto error;
2253 }
28ab034a 2254 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
04c17253 2255 ret = LTTNG_ERR_INVALID;
04c17253
MD
2256 goto error;
2257 }
2f77fc4b 2258
28ab034a 2259 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
f73fabfd 2260 if (ret != LTTNG_OK) {
2f77fc4b
DG
2261 goto error;
2262 }
2f77fc4b
DG
2263
2264 /* Get the newly created channel reference back */
28ab034a
JG
2265 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2266 channel_name);
a0377dfe 2267 LTTNG_ASSERT(uchan);
2f77fc4b
DG
2268 }
2269
141feb8c
JG
2270 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2271 /*
2272 * Don't allow users to add UST events to channels which
2273 * are assigned to a userspace subdomain (JUL, Log4J,
2274 * Python, etc.).
2275 */
2276 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2277 goto error;
2278 }
2279
dac8e046
JG
2280 if (!internal_event) {
2281 /*
2282 * Ensure the event name is not reserved for internal
2283 * use.
2284 */
2285 ret = validate_ust_event_name(event->name);
2286 if (ret) {
28ab034a 2287 WARN("Userspace event name %s failed validation.", event->name);
dac8e046
JG
2288 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2289 goto error;
2290 }
2291 }
2292
2f77fc4b 2293 /* At this point, the session and channel exist on the tracer */
28ab034a
JG
2294 ret = event_ust_enable_tracepoint(
2295 usess, uchan, event, filter_expression, filter, exclusion, internal_event);
49d21f93 2296 /* We have passed ownership */
cd9adb8b
JG
2297 filter_expression = nullptr;
2298 filter = nullptr;
2299 exclusion = nullptr;
94382e15
JG
2300 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2301 goto already_enabled;
2302 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2303 goto error;
2304 }
2305 break;
2306 }
5cdb6027 2307 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2308 case LTTNG_DOMAIN_JUL:
0e115563 2309 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2310 {
da6c3a50 2311 const char *default_event_name, *default_chan_name;
fefd409b 2312 struct agent *agt;
f20baf8e
DG
2313 struct lttng_event uevent;
2314 struct lttng_domain tmp_dom;
2315 struct ltt_ust_session *usess = session->ust_session;
2316
a0377dfe 2317 LTTNG_ASSERT(usess);
f20baf8e 2318
f28f9e44
JG
2319 if (!agent_tracing_is_enabled()) {
2320 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2321 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2322 goto error;
2323 }
2324
5cdb6027 2325 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2326 if (!agt) {
5cdb6027 2327 agt = agent_create(domain->type);
fefd409b 2328 if (!agt) {
e5b3c48c 2329 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2330 goto error;
2331 }
2332 agent_add(agt, usess->agents);
2333 }
2334
022d91ba 2335 /* Create the default tracepoint. */
996de3c7 2336 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2337 uevent.type = LTTNG_EVENT_TRACEPOINT;
2338 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
dcd24bbf 2339 uevent.loglevel = -1;
28ab034a 2340 default_event_name = event_get_default_agent_ust_name(domain->type);
da6c3a50 2341 if (!default_event_name) {
e5b3c48c 2342 ret = LTTNG_ERR_FATAL;
da6c3a50 2343 goto error;
f43f95a9 2344 }
da6c3a50 2345 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2346 uevent.name[sizeof(uevent.name) - 1] = '\0';
2347
2348 /*
2349 * The domain type is changed because we are about to enable the
2350 * default channel and event for the JUL domain that are hardcoded.
2351 * This happens in the UST domain.
2352 */
2353 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2354 tmp_dom.type = LTTNG_DOMAIN_UST;
2355
0e115563
DG
2356 switch (domain->type) {
2357 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2358 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2359 break;
2360 case LTTNG_DOMAIN_JUL:
da6c3a50 2361 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2362 break;
2363 case LTTNG_DOMAIN_PYTHON:
2364 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2365 break;
2366 default:
e98a44b0 2367 /* The switch/case we are in makes this impossible */
a0377dfe 2368 abort();
da6c3a50
DG
2369 }
2370
971da06a 2371 {
cd9adb8b
JG
2372 char *filter_expression_copy = nullptr;
2373 struct lttng_bytecode *filter_copy = nullptr;
971da06a
JG
2374
2375 if (filter) {
28ab034a
JG
2376 const size_t filter_size =
2377 sizeof(struct lttng_bytecode) + filter->len;
51755dc8 2378
64803277 2379 filter_copy = zmalloc<lttng_bytecode>(filter_size);
971da06a 2380 if (!filter_copy) {
018096a4 2381 ret = LTTNG_ERR_NOMEM;
b742e3e2 2382 goto error;
971da06a 2383 }
51755dc8 2384 memcpy(filter_copy, filter, filter_size);
971da06a 2385
28ab034a 2386 filter_expression_copy = strdup(filter_expression);
8404118c
JG
2387 if (!filter_expression) {
2388 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2389 }
2390
2391 if (!filter_expression_copy || !filter_copy) {
2392 free(filter_expression_copy);
2393 free(filter_copy);
2394 goto error;
8404118c 2395 }
971da06a
JG
2396 }
2397
28ab034a
JG
2398 ret = cmd_enable_event_internal(session,
2399 &tmp_dom,
2400 (char *) default_chan_name,
2401 &uevent,
2402 filter_expression_copy,
2403 filter_copy,
cd9adb8b 2404 nullptr,
28ab034a 2405 wpipe);
971da06a
JG
2406 }
2407
94382e15
JG
2408 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2409 goto already_enabled;
2410 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2411 goto error;
2412 }
2413
2414 /* The wild card * means that everything should be enabled. */
2415 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
28ab034a 2416 ret = event_agent_enable_all(usess, agt, event, filter, filter_expression);
f20baf8e 2417 } else {
28ab034a 2418 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
f20baf8e 2419 }
cd9adb8b
JG
2420 filter = nullptr;
2421 filter_expression = nullptr;
f20baf8e
DG
2422 if (ret != LTTNG_OK) {
2423 goto error;
2424 }
2425
2426 break;
2427 }
2f77fc4b 2428 default:
f73fabfd 2429 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2430 goto error;
2431 }
2432
f73fabfd 2433 ret = LTTNG_OK;
2f77fc4b 2434
94382e15 2435already_enabled:
2f77fc4b 2436error:
49d21f93
MD
2437 free(filter_expression);
2438 free(filter);
2439 free(exclusion);
cf0bcb51 2440 channel_attr_destroy(attr);
2f77fc4b
DG
2441 return ret;
2442}
2443
88f06f15
JG
2444/*
2445 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2446 * We own filter, exclusion, and filter_expression.
2447 */
8ddd72ef 2448int cmd_enable_event(struct command_ctx *cmd_ctx,
28ab034a
JG
2449 struct lttng_event *event,
2450 char *filter_expression,
2451 struct lttng_event_exclusion *exclusion,
2452 struct lttng_bytecode *bytecode,
2453 int wpipe)
88f06f15 2454{
8ddd72ef
JR
2455 int ret;
2456 /*
2457 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2458 */
2459 const lttng_domain command_domain = cmd_ctx->lsm.domain;
2460
2461 /*
2462 * The ownership of the following parameters is transferred to
2463 * _cmd_enable_event:
2464 *
2465 * - filter_expression,
2466 * - bytecode,
2467 * - exclusion
2468 */
2469 ret = _cmd_enable_event(cmd_ctx->session,
28ab034a
JG
2470 &command_domain,
2471 cmd_ctx->lsm.u.enable.channel_name,
2472 event,
2473 filter_expression,
2474 bytecode,
2475 exclusion,
2476 wpipe,
2477 false);
cd9adb8b
JG
2478 filter_expression = nullptr;
2479 bytecode = nullptr;
2480 exclusion = nullptr;
8ddd72ef 2481 return ret;
88f06f15
JG
2482}
2483
2484/*
2485 * Enable an event which is internal to LTTng. An internal should
2486 * never be made visible to clients and are immune to checks such as
2487 * reserved names.
2488 */
2489static int cmd_enable_event_internal(struct ltt_session *session,
28ab034a
JG
2490 const struct lttng_domain *domain,
2491 char *channel_name,
2492 struct lttng_event *event,
2493 char *filter_expression,
2494 struct lttng_bytecode *filter,
2495 struct lttng_event_exclusion *exclusion,
2496 int wpipe)
88f06f15 2497{
28ab034a
JG
2498 return _cmd_enable_event(session,
2499 domain,
2500 channel_name,
2501 event,
2502 filter_expression,
2503 filter,
2504 exclusion,
2505 wpipe,
2506 true);
88f06f15
JG
2507}
2508
2f77fc4b
DG
2509/*
2510 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2511 */
8ddd72ef 2512enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
28ab034a 2513 struct lttng_payload *reply_payload)
2f77fc4b 2514{
8ddd72ef 2515 enum lttng_error_code ret_code;
2f77fc4b 2516 int ret;
8ddd72ef 2517 ssize_t i, nb_events = 0;
cd9adb8b 2518 struct lttng_event *events = nullptr;
8ddd72ef
JR
2519 struct lttcomm_list_command_header reply_command_header = {};
2520 size_t reply_command_header_offset;
2521
2522 assert(reply_payload);
2523
2524 /* Reserve space for command reply header. */
2525 reply_command_header_offset = reply_payload->buffer.size;
2526 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
28ab034a
JG
2527 reply_command_header_offset +
2528 sizeof(struct lttcomm_list_command_header));
8ddd72ef
JR
2529 if (ret) {
2530 ret_code = LTTNG_ERR_NOMEM;
2531 goto error;
2532 }
2f77fc4b
DG
2533
2534 switch (domain) {
2535 case LTTNG_DOMAIN_KERNEL:
8ddd72ef 2536 nb_events = kernel_list_events(&events);
2f77fc4b 2537 if (nb_events < 0) {
8ddd72ef 2538 ret_code = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
2539 goto error;
2540 }
2541 break;
2542 case LTTNG_DOMAIN_UST:
8ddd72ef 2543 nb_events = ust_app_list_events(&events);
2f77fc4b 2544 if (nb_events < 0) {
8ddd72ef 2545 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2546 goto error;
2547 }
2548 break;
5cdb6027 2549 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2550 case LTTNG_DOMAIN_JUL:
0e115563 2551 case LTTNG_DOMAIN_PYTHON:
8ddd72ef 2552 nb_events = agent_list_events(&events, domain);
3c6a091f 2553 if (nb_events < 0) {
8ddd72ef 2554 ret_code = LTTNG_ERR_UST_LIST_FAIL;
3c6a091f
DG
2555 goto error;
2556 }
2557 break;
2f77fc4b 2558 default:
8ddd72ef
JR
2559 ret_code = LTTNG_ERR_UND;
2560 goto error;
2561 }
2562
2563 for (i = 0; i < nb_events; i++) {
cd9adb8b
JG
2564 ret = lttng_event_serialize(
2565 &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
8ddd72ef
JR
2566 if (ret) {
2567 ret_code = LTTNG_ERR_NOMEM;
2568 goto error;
2569 }
2570 }
2571
2572 if (nb_events > UINT32_MAX) {
2573 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2574 ret_code = LTTNG_ERR_OVERFLOW;
2f77fc4b
DG
2575 goto error;
2576 }
2577
8ddd72ef
JR
2578 /* Update command reply header. */
2579 reply_command_header.count = (uint32_t) nb_events;
28ab034a
JG
2580 memcpy(reply_payload->buffer.data + reply_command_header_offset,
2581 &reply_command_header,
2582 sizeof(reply_command_header));
2f77fc4b 2583
8ddd72ef 2584 ret_code = LTTNG_OK;
2f77fc4b 2585error:
8ddd72ef
JR
2586 free(events);
2587 return ret_code;
2f77fc4b
DG
2588}
2589
2590/*
2591 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2592 */
b2d68839 2593enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain,
28ab034a 2594 struct lttng_payload *reply)
2f77fc4b 2595{
b2d68839 2596 enum lttng_error_code ret_code;
2f77fc4b 2597 int ret;
b2d68839 2598 unsigned int i, nb_fields;
cd9adb8b 2599 struct lttng_event_field *fields = nullptr;
b2d68839
JR
2600 struct lttcomm_list_command_header reply_command_header = {};
2601 size_t reply_command_header_offset;
2602
2603 assert(reply);
2604
2605 /* Reserve space for command reply header. */
2606 reply_command_header_offset = reply->buffer.size;
2607 ret = lttng_dynamic_buffer_set_size(&reply->buffer,
28ab034a
JG
2608 reply_command_header_offset +
2609 sizeof(struct lttcomm_list_command_header));
b2d68839
JR
2610 if (ret) {
2611 ret_code = LTTNG_ERR_NOMEM;
2612 goto error;
2613 }
2f77fc4b
DG
2614
2615 switch (domain) {
2616 case LTTNG_DOMAIN_UST:
b2d68839
JR
2617 ret = ust_app_list_event_fields(&fields);
2618 if (ret < 0) {
2619 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2620 goto error;
2621 }
b2d68839 2622
2f77fc4b
DG
2623 break;
2624 case LTTNG_DOMAIN_KERNEL:
28ab034a 2625 default: /* fall-through */
b2d68839 2626 ret_code = LTTNG_ERR_UND;
2f77fc4b
DG
2627 goto error;
2628 }
2629
b2d68839
JR
2630 nb_fields = ret;
2631
2632 for (i = 0; i < nb_fields; i++) {
2633 ret = lttng_event_field_serialize(&fields[i], reply);
2634 if (ret) {
2635 ret_code = LTTNG_ERR_NOMEM;
2636 goto error;
2637 }
2638 }
2639
2640 if (nb_fields > UINT32_MAX) {
2641 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2642 ret_code = LTTNG_ERR_OVERFLOW;
2643 goto error;
2644 }
2645
2646 /* Update command reply header. */
2647 reply_command_header.count = (uint32_t) nb_fields;
2648
28ab034a
JG
2649 memcpy(reply->buffer.data + reply_command_header_offset,
2650 &reply_command_header,
2651 sizeof(reply_command_header));
b2d68839
JR
2652
2653 ret_code = LTTNG_OK;
2f77fc4b
DG
2654
2655error:
b2d68839
JR
2656 free(fields);
2657 return ret_code;
2f77fc4b
DG
2658}
2659
28ab034a 2660enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload)
834978fd 2661{
8ddd72ef
JR
2662 enum lttng_error_code ret_code;
2663 ssize_t nb_events, i;
2664 int ret;
cd9adb8b 2665 struct lttng_event *events = nullptr;
8ddd72ef
JR
2666 struct lttcomm_list_command_header reply_command_header = {};
2667 size_t reply_command_header_offset;
2668
2669 assert(reply_payload);
2670
2671 /* Reserve space for command reply header. */
2672 reply_command_header_offset = reply_payload->buffer.size;
2673 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
28ab034a
JG
2674 reply_command_header_offset +
2675 sizeof(struct lttcomm_list_command_header));
8ddd72ef
JR
2676 if (ret) {
2677 ret_code = LTTNG_ERR_NOMEM;
2678 goto end;
2679 }
2680
2681 nb_events = syscall_table_list(&events);
2682 if (nb_events < 0) {
28ab034a 2683 ret_code = (enum lttng_error_code) - nb_events;
8ddd72ef
JR
2684 goto end;
2685 }
2686
2687 for (i = 0; i < nb_events; i++) {
cd9adb8b
JG
2688 ret = lttng_event_serialize(
2689 &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
8ddd72ef
JR
2690 if (ret) {
2691 ret_code = LTTNG_ERR_NOMEM;
2692 goto end;
2693 }
2694 }
2695
2696 if (nb_events > UINT32_MAX) {
2697 ERR("Syscall count would overflow the syscall listing command's reply");
2698 ret_code = LTTNG_ERR_OVERFLOW;
2699 goto end;
2700 }
2701
2702 /* Update command reply header. */
2703 reply_command_header.count = (uint32_t) nb_events;
28ab034a
JG
2704 memcpy(reply_payload->buffer.data + reply_command_header_offset,
2705 &reply_command_header,
2706 sizeof(reply_command_header));
8ddd72ef
JR
2707
2708 ret_code = LTTNG_OK;
2709end:
2710 free(events);
2711 return ret_code;
834978fd
DG
2712}
2713
2f77fc4b
DG
2714/*
2715 * Command LTTNG_START_TRACE processed by the client thread.
a9ad0c8f
MD
2716 *
2717 * Called with session mutex held.
2f77fc4b
DG
2718 */
2719int cmd_start_trace(struct ltt_session *session)
2720{
82b69413 2721 enum lttng_error_code ret;
cde3e505 2722 unsigned long nb_chan = 0;
2f77fc4b
DG
2723 struct ltt_kernel_session *ksession;
2724 struct ltt_ust_session *usess;
28ab034a
JG
2725 const bool session_rotated_after_last_stop = session->rotated_after_last_stop;
2726 const bool session_cleared_after_last_stop = session->cleared_after_last_stop;
2f77fc4b 2727
a0377dfe 2728 LTTNG_ASSERT(session);
2f77fc4b
DG
2729
2730 /* Ease our life a bit ;) */
2731 ksession = session->kernel_session;
2732 usess = session->ust_session;
2733
8382cf6f
DG
2734 /* Is the session already started? */
2735 if (session->active) {
f73fabfd 2736 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
7a24ece3
JR
2737 /* Perform nothing */
2738 goto end;
2f77fc4b
DG
2739 }
2740
1f496244 2741 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
28ab034a 2742 !session->current_trace_chunk) {
1f496244
JG
2743 /*
2744 * A rotation was launched while the session was stopped and
2745 * it has not been completed yet. It is not possible to start
2746 * the session since starting the session here would require a
2747 * rotation from "NULL" to a new trace chunk. That rotation
2748 * would overlap with the ongoing rotation, which is not
2749 * supported.
2750 */
2751 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
28ab034a 2752 session->name);
1f496244
JG
2753 ret = LTTNG_ERR_ROTATION_PENDING;
2754 goto error;
2755 }
2756
cde3e505
DG
2757 /*
2758 * Starting a session without channel is useless since after that it's not
2759 * possible to enable channel thus inform the client.
2760 */
2761 if (usess && usess->domain_global.channels) {
2762 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2763 }
2764 if (ksession) {
2765 nb_chan += ksession->channel_count;
2766 }
2767 if (!nb_chan) {
2768 ret = LTTNG_ERR_NO_CHANNEL;
2769 goto error;
2770 }
2771
66cefebd 2772 session->active = true;
1f496244 2773 session->rotated_after_last_stop = false;
b02f5986 2774 session->cleared_after_last_stop = false;
070b6a86 2775 if (session->output_traces && !session->current_trace_chunk) {
1f496244
JG
2776 if (!session->has_been_started) {
2777 struct lttng_trace_chunk *trace_chunk;
2778
28ab034a 2779 DBG("Creating initial trace chunk of session \"%s\"", session->name);
cd9adb8b
JG
2780 trace_chunk =
2781 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
1f496244
JG
2782 if (!trace_chunk) {
2783 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2784 goto error;
2785 }
a0377dfe 2786 LTTNG_ASSERT(!session->current_trace_chunk);
28ab034a 2787 ret = (lttng_error_code) session_set_trace_chunk(
cd9adb8b 2788 session, trace_chunk, nullptr);
1f496244
JG
2789 lttng_trace_chunk_put(trace_chunk);
2790 if (ret) {
2791 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2792 goto error;
2793 }
2794 } else {
2795 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
28ab034a 2796 session->name);
1f496244
JG
2797 /*
2798 * Rotate existing streams into the new chunk.
2799 * This is a "quiet" rotation has no client has
2800 * explicitly requested this operation.
2801 *
2802 * There is also no need to wait for the rotation
2803 * to complete as it will happen immediately. No data
2804 * was produced as the session was stopped, so the
2805 * rotation should happen on reception of the command.
2806 */
28ab034a 2807 ret = (lttng_error_code) cmd_rotate_session(
cd9adb8b 2808 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
1f496244
JG
2809 if (ret != LTTNG_OK) {
2810 goto error;
2811 }
5c408ad8 2812 }
c996624c
JD
2813 }
2814
2f77fc4b 2815 /* Kernel tracing */
cd9adb8b 2816 if (ksession != nullptr) {
c996624c 2817 DBG("Start kernel tracing session %s", session->name);
7966af57 2818 ret = (lttng_error_code) start_kernel_session(ksession);
9b6c7ec5 2819 if (ret != LTTNG_OK) {
2f77fc4b
DG
2820 goto error;
2821 }
2f77fc4b
DG
2822 }
2823
2824 /* Flag session that trace should start automatically */
2825 if (usess) {
82b69413
JG
2826 int int_ret = ust_app_start_trace_all(usess);
2827
2828 if (int_ret < 0) {
f73fabfd 2829 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
2830 goto error;
2831 }
2832 }
2833
04ed9e10
JG
2834 /*
2835 * Open a packet in every stream of the session to ensure that viewers
2836 * can correctly identify the boundaries of the periods during which
2837 * tracing was active for this session.
2838 */
2839 ret = session_open_packets(session);
2840 if (ret != LTTNG_OK) {
2841 goto error;
2842 }
2843
5c408ad8
JD
2844 /*
2845 * Clear the flag that indicates that a rotation was done while the
2846 * session was stopped.
2847 */
2848 session->rotated_after_last_stop = false;
2849
355cf1bd 2850 if (session->rotate_timer_period && !session->rotation_schedule_timer_enabled) {
82b69413 2851 int int_ret = timer_session_rotation_schedule_timer_start(
28ab034a 2852 session, session->rotate_timer_period);
82b69413
JG
2853
2854 if (int_ret < 0) {
259c2674
JD
2855 ERR("Failed to enable rotate timer");
2856 ret = LTTNG_ERR_UNK;
2857 goto error;
2858 }
2859 }
2860
f73fabfd 2861 ret = LTTNG_OK;
2f77fc4b
DG
2862
2863error:
1f496244
JG
2864 if (ret == LTTNG_OK) {
2865 /* Flag this after a successful start. */
66cefebd 2866 session->has_been_started = true;
1f496244 2867 } else {
66cefebd 2868 session->active = false;
1f496244 2869 /* Restore initial state on error. */
28ab034a
JG
2870 session->rotated_after_last_stop = session_rotated_after_last_stop;
2871 session->cleared_after_last_stop = session_cleared_after_last_stop;
1f496244 2872 }
7a24ece3 2873end:
2f77fc4b
DG
2874 return ret;
2875}
2876
2877/*
2878 * Command LTTNG_STOP_TRACE processed by the client thread.
2879 */
2880int cmd_stop_trace(struct ltt_session *session)
2881{
2882 int ret;
2f77fc4b
DG
2883 struct ltt_kernel_session *ksession;
2884 struct ltt_ust_session *usess;
2885
a0377dfe 2886 LTTNG_ASSERT(session);
2f77fc4b 2887
4dbe1875 2888 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2f77fc4b
DG
2889 /* Short cut */
2890 ksession = session->kernel_session;
2891 usess = session->ust_session;
2892
40afd77d 2893 /* Session is not active. Skip everything and inform the client. */
8382cf6f 2894 if (!session->active) {
f73fabfd 2895 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
2896 goto error;
2897 }
2898
4dbe1875
MD
2899 ret = stop_kernel_session(ksession);
2900 if (ret != LTTNG_OK) {
2901 goto error;
2f77fc4b
DG
2902 }
2903
14fb1ebe 2904 if (usess && usess->active) {
2f77fc4b
DG
2905 ret = ust_app_stop_trace_all(usess);
2906 if (ret < 0) {
f73fabfd 2907 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2908 goto error;
2909 }
2910 }
2911
28ab034a 2912 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
8382cf6f 2913 /* Flag inactive after a successful stop. */
66cefebd 2914 session->active = false;
4dbe1875 2915 ret = LTTNG_OK;
2f77fc4b
DG
2916
2917error:
2918 return ret;
2919}
2920
2921/*
433f5ba9
JR
2922 * Set the base_path of the session only if subdir of a control uris is set.
2923 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2f77fc4b 2924 */
28ab034a
JG
2925static int
2926set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
2f77fc4b 2927{
433f5ba9
JR
2928 int ret;
2929 size_t i;
2f77fc4b 2930
e3876bf0 2931 for (i = 0; i < nb_uri; i++) {
28ab034a 2932 if (uris[i].stype != LTTNG_STREAM_CONTROL || uris[i].subdir[0] == '\0') {
e3876bf0
JR
2933 /* Not interested in these URIs */
2934 continue;
2935 }
2936
cd9adb8b 2937 if (session->base_path != nullptr) {
e3876bf0 2938 free(session->base_path);
cd9adb8b 2939 session->base_path = nullptr;
e3876bf0
JR
2940 }
2941
2942 /* Set session base_path */
2943 session->base_path = strdup(uris[i].subdir);
2944 if (!session->base_path) {
433f5ba9 2945 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
28ab034a
JG
2946 uris[i].subdir,
2947 session->name);
433f5ba9 2948 ret = LTTNG_ERR_NOMEM;
e3876bf0
JR
2949 goto error;
2950 }
433f5ba9 2951 DBG2("Setting base path \"%s\" for session \"%s\"",
28ab034a
JG
2952 session->base_path,
2953 session->name);
433f5ba9
JR
2954 }
2955 ret = LTTNG_OK;
2956error:
2957 return ret;
2958}
2959
2960/*
2961 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2962 */
28ab034a 2963int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
433f5ba9
JR
2964{
2965 int ret, i;
2966 struct ltt_kernel_session *ksess = session->kernel_session;
2967 struct ltt_ust_session *usess = session->ust_session;
2968
a0377dfe
FD
2969 LTTNG_ASSERT(session);
2970 LTTNG_ASSERT(uris);
2971 LTTNG_ASSERT(nb_uri > 0);
433f5ba9
JR
2972
2973 /* Can't set consumer URI if the session is active. */
2974 if (session->active) {
2975 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2976 goto error;
2977 }
2978
2979 /*
2980 * Set the session base path if any. This is done inside
2981 * cmd_set_consumer_uri to preserve backward compatibility of the
2982 * previous session creation api vs the session descriptor api.
2983 */
2984 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2985 if (ret != LTTNG_OK) {
2986 goto error;
e3876bf0
JR
2987 }
2988
bda32d56 2989 /* Set the "global" consumer URIs */
2f77fc4b 2990 for (i = 0; i < nb_uri; i++) {
28ab034a 2991 ret = add_uri_to_consumer(session, session->consumer, &uris[i], LTTNG_DOMAIN_NONE);
a74934ba 2992 if (ret != LTTNG_OK) {
2f77fc4b
DG
2993 goto error;
2994 }
2f77fc4b
DG
2995 }
2996
bda32d56
JG
2997 /* Set UST session URIs */
2998 if (session->ust_session) {
2999 for (i = 0; i < nb_uri; i++) {
b178f53e 3000 ret = add_uri_to_consumer(session,
28ab034a
JG
3001 session->ust_session->consumer,
3002 &uris[i],
3003 LTTNG_DOMAIN_UST);
bda32d56
JG
3004 if (ret != LTTNG_OK) {
3005 goto error;
3006 }
3007 }
3008 }
3009
3010 /* Set kernel session URIs */
3011 if (session->kernel_session) {
3012 for (i = 0; i < nb_uri; i++) {
b178f53e 3013 ret = add_uri_to_consumer(session,
28ab034a
JG
3014 session->kernel_session->consumer,
3015 &uris[i],
3016 LTTNG_DOMAIN_KERNEL);
bda32d56
JG
3017 if (ret != LTTNG_OK) {
3018 goto error;
3019 }
3020 }
3021 }
3022
7ab70fe0
DG
3023 /*
3024 * Make sure to set the session in output mode after we set URI since a
3025 * session can be created without URL (thus flagged in no output mode).
3026 */
3027 session->output_traces = 1;
3028 if (ksess) {
3029 ksess->output_traces = 1;
bda32d56
JG
3030 }
3031
3032 if (usess) {
7ab70fe0
DG
3033 usess->output_traces = 1;
3034 }
3035
2f77fc4b 3036 /* All good! */
f73fabfd 3037 ret = LTTNG_OK;
2f77fc4b
DG
3038
3039error:
3040 return ret;
3041}
3042
28ab034a
JG
3043static enum lttng_error_code
3044set_session_output_from_descriptor(struct ltt_session *session,
3045 const struct lttng_session_descriptor *descriptor)
2f77fc4b
DG
3046{
3047 int ret;
b178f53e
JG
3048 enum lttng_error_code ret_code = LTTNG_OK;
3049 enum lttng_session_descriptor_type session_type =
28ab034a 3050 lttng_session_descriptor_get_type(descriptor);
b178f53e 3051 enum lttng_session_descriptor_output_type output_type =
28ab034a 3052 lttng_session_descriptor_get_output_type(descriptor);
b178f53e
JG
3053 struct lttng_uri uris[2] = {};
3054 size_t uri_count = 0;
3055
3056 switch (output_type) {
3057 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3058 goto end;
3059 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
28ab034a 3060 lttng_session_descriptor_get_local_output_uri(descriptor, &uris[0]);
b178f53e
JG
3061 uri_count = 1;
3062 break;
3063 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
28ab034a 3064 lttng_session_descriptor_get_network_output_uris(descriptor, &uris[0], &uris[1]);
b178f53e
JG
3065 uri_count = 2;
3066 break;
3067 default:
3068 ret_code = LTTNG_ERR_INVALID;
e32d7f27 3069 goto end;
2f77fc4b
DG
3070 }
3071
b178f53e
JG
3072 switch (session_type) {
3073 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3074 {
cd9adb8b 3075 struct snapshot_output *new_output = nullptr;
b178f53e
JG
3076
3077 new_output = snapshot_output_alloc();
3078 if (!new_output) {
3079 ret_code = LTTNG_ERR_NOMEM;
3080 goto end;
3081 }
3082
3083 ret = snapshot_output_init_with_uri(session,
28ab034a 3084 DEFAULT_SNAPSHOT_MAX_SIZE,
cd9adb8b 3085 nullptr,
28ab034a
JG
3086 uris,
3087 uri_count,
3088 session->consumer,
3089 new_output,
3090 &session->snapshot);
b178f53e 3091 if (ret < 0) {
28ab034a 3092 ret_code = (ret == -ENOMEM) ? LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
b178f53e
JG
3093 snapshot_output_destroy(new_output);
3094 goto end;
3095 }
3096 snapshot_add_output(&session->snapshot, new_output);
3097 break;
3098 }
3099 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3100 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3101 {
7966af57 3102 ret_code = (lttng_error_code) cmd_set_consumer_uri(session, uri_count, uris);
b178f53e
JG
3103 break;
3104 }
3105 default:
3106 ret_code = LTTNG_ERR_INVALID;
e32d7f27 3107 goto end;
2f77fc4b 3108 }
b178f53e
JG
3109end:
3110 return ret_code;
3111}
3112
28ab034a
JG
3113static enum lttng_error_code
3114cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
3115 const lttng_sock_cred *creds,
3116 const char *home_path)
b178f53e
JG
3117{
3118 int ret;
3119 enum lttng_error_code ret_code;
3120 const char *session_name;
cd9adb8b 3121 struct ltt_session *new_session = nullptr;
b178f53e 3122 enum lttng_session_descriptor_status descriptor_status;
2f77fc4b 3123
e32d7f27 3124 session_lock_list();
b178f53e
JG
3125 if (home_path) {
3126 if (*home_path != '/') {
3127 ERR("Home path provided by client is not absolute");
3128 ret_code = LTTNG_ERR_INVALID;
3129 goto end;
3130 }
3131 }
2f77fc4b 3132
28ab034a 3133 descriptor_status = lttng_session_descriptor_get_session_name(descriptor, &session_name);
b178f53e
JG
3134 switch (descriptor_status) {
3135 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3136 break;
3137 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
cd9adb8b 3138 session_name = nullptr;
b178f53e
JG
3139 break;
3140 default:
3141 ret_code = LTTNG_ERR_INVALID;
3142 goto end;
3143 }
e3876bf0 3144
28ab034a 3145 ret_code = session_create(session_name, creds->uid, creds->gid, &new_session);
b178f53e 3146 if (ret_code != LTTNG_OK) {
e32d7f27 3147 goto end;
2f77fc4b
DG
3148 }
3149
139a8d25 3150 ret_code = notification_thread_command_add_session(the_notification_thread_handle,
28ab034a
JG
3151 new_session->id,
3152 new_session->name,
3153 new_session->uid,
3154 new_session->gid);
139a8d25
JG
3155 if (ret_code != LTTNG_OK) {
3156 goto end;
3157 }
3158
3159 /* Announce the session's destruction to the notification thread when it is destroyed. */
3160 ret = session_add_destroy_notifier(
28ab034a
JG
3161 new_session,
3162 [](const struct ltt_session *session, void *user_data __attribute__((unused))) {
3163 (void) notification_thread_command_remove_session(
3164 the_notification_thread_handle, session->id);
3165 },
cd9adb8b 3166 nullptr);
139a8d25
JG
3167 if (ret) {
3168 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
28ab034a 3169 new_session->name);
139a8d25
JG
3170 ret = LTTNG_ERR_NOMEM;
3171 goto end;
3172 }
3173
b178f53e 3174 if (!session_name) {
28ab034a 3175 ret = lttng_session_descriptor_set_session_name(descriptor, new_session->name);
b178f53e
JG
3176 if (ret) {
3177 ret_code = LTTNG_ERR_SESSION_FAIL;
3178 goto end;
3179 }
3180 }
3181
28ab034a 3182 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor)) {
b178f53e
JG
3183 /*
3184 * Only include the session's creation time in the output
3185 * destination if the name of the session itself was
3186 * not auto-generated.
3187 */
3188 ret_code = lttng_session_descriptor_set_default_output(
cd9adb8b
JG
3189 descriptor,
3190 session_name ? &new_session->creation_time : nullptr,
3191 home_path);
b178f53e 3192 if (ret_code != LTTNG_OK) {
e32d7f27 3193 goto end;
2bba9e53 3194 }
2bba9e53 3195 } else {
b178f53e 3196 new_session->has_user_specified_directory =
28ab034a 3197 lttng_session_descriptor_has_output_directory(descriptor);
2f77fc4b
DG
3198 }
3199
b178f53e
JG
3200 switch (lttng_session_descriptor_get_type(descriptor)) {
3201 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3202 new_session->snapshot_mode = 1;
3203 break;
3204 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3205 new_session->live_timer =
28ab034a 3206 lttng_session_descriptor_live_get_timer_interval(descriptor);
b178f53e
JG
3207 break;
3208 default:
3209 break;
3210 }
2f77fc4b 3211
b178f53e
JG
3212 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3213 if (ret_code != LTTNG_OK) {
3214 goto end;
3215 }
66cefebd 3216 new_session->consumer->enabled = true;
b178f53e 3217 ret_code = LTTNG_OK;
e32d7f27 3218end:
b178f53e
JG
3219 /* Release reference provided by the session_create function. */
3220 session_put(new_session);
3221 if (ret_code != LTTNG_OK && new_session) {
3222 /* Release the global reference on error. */
3223 session_destroy(new_session);
e32d7f27 3224 }
b178f53e
JG
3225 session_unlock_list();
3226 return ret_code;
2f77fc4b
DG
3227}
3228
28ab034a
JG
3229enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx,
3230 int sock,
3231 struct lttng_session_descriptor **return_descriptor)
27babd3a
DG
3232{
3233 int ret;
b178f53e
JG
3234 size_t payload_size;
3235 struct lttng_dynamic_buffer payload;
3236 struct lttng_buffer_view home_dir_view;
3237 struct lttng_buffer_view session_descriptor_view;
cd9adb8b 3238 struct lttng_session_descriptor *session_descriptor = nullptr;
b178f53e
JG
3239 enum lttng_error_code ret_code;
3240
3241 lttng_dynamic_buffer_init(&payload);
28ab034a 3242 if (cmd_ctx->lsm.u.create_session.home_dir_size >= LTTNG_PATH_MAX) {
b178f53e
JG
3243 ret_code = LTTNG_ERR_INVALID;
3244 goto error;
27babd3a 3245 }
3a91de3a 3246 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
28ab034a 3247 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
b178f53e
JG
3248 ret_code = LTTNG_ERR_INVALID;
3249 goto error;
27babd3a
DG
3250 }
3251
3a91de3a 3252 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
28ab034a 3253 cmd_ctx->lsm.u.create_session.session_descriptor_size;
b178f53e
JG
3254 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3255 if (ret) {
3256 ret_code = LTTNG_ERR_NOMEM;
3257 goto error;
27babd3a
DG
3258 }
3259
b178f53e
JG
3260 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3261 if (ret <= 0) {
3262 ERR("Reception of session descriptor failed, aborting.");
3263 ret_code = LTTNG_ERR_SESSION_FAIL;
3264 goto error;
27babd3a
DG
3265 }
3266
b178f53e 3267 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
28ab034a 3268 &payload, 0, cmd_ctx->lsm.u.create_session.home_dir_size);
3e6e0df2 3269 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
28ab034a 3270 !lttng_buffer_view_is_valid(&home_dir_view)) {
3e6e0df2
JG
3271 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3272 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3273 goto error;
3274 }
3275
b178f53e 3276 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
28ab034a
JG
3277 &payload,
3278 cmd_ctx->lsm.u.create_session.home_dir_size,
3279 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3e6e0df2
JG
3280 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3281 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3282 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3283 goto error;
3284 }
27babd3a 3285
28ab034a
JG
3286 ret = lttng_session_descriptor_create_from_buffer(&session_descriptor_view,
3287 &session_descriptor);
b178f53e
JG
3288 if (ret < 0) {
3289 ERR("Failed to create session descriptor from payload of \"create session\" command");
3290 ret_code = LTTNG_ERR_INVALID;
3291 goto error;
3292 }
27babd3a 3293
b178f53e
JG
3294 /*
3295 * Sets the descriptor's auto-generated properties (name, output) if
3296 * needed.
3297 */
cd9adb8b
JG
3298 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3299 &cmd_ctx->creds,
3300 home_dir_view.size ? home_dir_view.data :
3301 nullptr);
b178f53e
JG
3302 if (ret_code != LTTNG_OK) {
3303 goto error;
e32d7f27 3304 }
b178f53e
JG
3305
3306 ret_code = LTTNG_OK;
3307 *return_descriptor = session_descriptor;
cd9adb8b 3308 session_descriptor = nullptr;
b178f53e
JG
3309error:
3310 lttng_dynamic_buffer_reset(&payload);
3311 lttng_session_descriptor_destroy(session_descriptor);
3312 return ret_code;
27babd3a
DG
3313}
3314
28ab034a 3315static void cmd_destroy_session_reply(const struct ltt_session *session, void *_reply_context)
3e3665b8
JG
3316{
3317 int ret;
3318 ssize_t comm_ret;
3319 const struct cmd_destroy_session_reply_context *reply_context =
28ab034a 3320 (cmd_destroy_session_reply_context *) _reply_context;
3e3665b8
JG
3321 struct lttng_dynamic_buffer payload;
3322 struct lttcomm_session_destroy_command_header cmd_header;
cd9adb8b 3323 struct lttng_trace_archive_location *location = nullptr;
3e3665b8 3324 struct lttcomm_lttng_msg llm = {
37a5ef39 3325 .cmd_type = LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION,
3285a971 3326 .ret_code = reply_context->destruction_status,
3e3665b8 3327 .pid = UINT32_MAX,
28ab034a 3328 .cmd_header_size = sizeof(struct lttcomm_session_destroy_command_header),
3e3665b8 3329 .data_size = 0,
1c9a0b0e 3330 .fd_count = 0,
3e3665b8
JG
3331 };
3332 size_t payload_size_before_location;
3333
3334 lttng_dynamic_buffer_init(&payload);
3335
3336 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
0e270a1e 3337 if (ret) {
3e3665b8
JG
3338 ERR("Failed to append session destruction message");
3339 goto error;
0e270a1e 3340 }
3e3665b8 3341
28ab034a
JG
3342 cmd_header.rotation_state = (int32_t) (reply_context->implicit_rotation_on_destroy ?
3343 session->rotation_state :
3344 LTTNG_ROTATION_STATE_NO_ROTATION);
3345 ret = lttng_dynamic_buffer_append(&payload, &cmd_header, sizeof(cmd_header));
3e3665b8
JG
3346 if (ret) {
3347 ERR("Failed to append session destruction command header");
3348 goto error;
3349 }
3350
3351 if (!reply_context->implicit_rotation_on_destroy) {
3352 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
28ab034a 3353 session->name);
3e3665b8
JG
3354 goto send_reply;
3355 }
3356 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3357 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
28ab034a 3358 session->name);
3e3665b8
JG
3359 goto send_reply;
3360 }
3361
3362 location = session_get_trace_archive_location(session);
3363 if (!location) {
3364 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
28ab034a 3365 session->name);
3e3665b8
JG
3366 goto error;
3367 }
3368
3369 payload_size_before_location = payload.size;
28ab034a 3370 comm_ret = lttng_trace_archive_location_serialize(location, &payload);
d3740619 3371 lttng_trace_archive_location_put(location);
3e3665b8
JG
3372 if (comm_ret < 0) {
3373 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
28ab034a 3374 session->name);
3e3665b8
JG
3375 goto error;
3376 }
3377 /* Update the message to indicate the location's length. */
3378 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
28ab034a 3379 payload.size - payload_size_before_location;
3e3665b8 3380send_reply:
28ab034a 3381 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, payload.data, payload.size);
3e3665b8
JG
3382 if (comm_ret != (ssize_t) payload.size) {
3383 ERR("Failed to send result of the destruction of session \"%s\" to client",
28ab034a 3384 session->name);
3e3665b8
JG
3385 }
3386error:
3387 ret = close(reply_context->reply_sock_fd);
3388 if (ret) {
3389 PERROR("Failed to close client socket in deferred session destroy reply");
3390 }
3391 lttng_dynamic_buffer_reset(&payload);
3392 free(_reply_context);
3393}
3394
2f77fc4b
DG
3395/*
3396 * Command LTTNG_DESTROY_SESSION processed by the client thread.
a9ad0c8f
MD
3397 *
3398 * Called with session lock held.
2f77fc4b 3399 */
28f23191 3400int cmd_destroy_session(struct ltt_session *session, int *sock_fd)
2f77fc4b
DG
3401{
3402 int ret;
3285a971 3403 enum lttng_error_code destruction_last_error = LTTNG_OK;
cd9adb8b 3404 struct cmd_destroy_session_reply_context *reply_context = nullptr;
3e3665b8
JG
3405
3406 if (sock_fd) {
64803277 3407 reply_context = zmalloc<cmd_destroy_session_reply_context>();
3e3665b8
JG
3408 if (!reply_context) {
3409 ret = LTTNG_ERR_NOMEM;
3410 goto end;
3411 }
64803277 3412
3e3665b8
JG
3413 reply_context->reply_sock_fd = *sock_fd;
3414 }
2f77fc4b
DG
3415
3416 /* Safety net */
a0377dfe 3417 LTTNG_ASSERT(session);
2f77fc4b 3418
28ab034a 3419 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id);
3e3665b8
JG
3420 if (session->active) {
3421 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
28ab034a 3422 session->name);
3e3665b8
JG
3423 ret = cmd_stop_trace(session);
3424 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3425 /* Carry on with the destruction of the session. */
3426 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
28ab034a
JG
3427 session->name,
3428 lttng_strerror(-ret));
7966af57 3429 destruction_last_error = (lttng_error_code) ret;
3e3665b8
JG
3430 }
3431 }
5c408ad8 3432
92816cc3 3433 if (session->rotation_schedule_timer_enabled) {
28ab034a 3434 if (timer_session_rotation_schedule_timer_stop(session)) {
92816cc3 3435 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
28ab034a 3436 session->name);
3285a971 3437 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
92816cc3 3438 }
259c2674
JD
3439 }
3440
90936dcf 3441 if (session->rotate_size) {
0038180d
JG
3442 try {
3443 the_rotation_thread_handle->unsubscribe_session_consumed_size_rotation(
3444 *session);
5b9eda8a 3445 } catch (const std::exception& e) {
0038180d
JG
3446 /* Continue the destruction of the session anyway. */
3447 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3448 e.what());
3449 }
3450
90936dcf
JD
3451 session->rotate_size = 0;
3452 }
3453
a7ceb342 3454 if (session->rotated && session->current_trace_chunk && session->output_traces) {
b5893d8e
JG
3455 /*
3456 * Perform a last rotation on destruction if rotations have
3457 * occurred during the session's lifetime.
3458 */
28ab034a 3459 ret = cmd_rotate_session(
cd9adb8b 3460 session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
d2956687
JG
3461 if (ret != LTTNG_OK) {
3462 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
28ab034a
JG
3463 session->name,
3464 lttng_strerror(-ret));
7966af57 3465 destruction_last_error = (lttng_error_code) -ret;
124473a3 3466 }
0e270a1e 3467 if (reply_context) {
3e3665b8 3468 reply_context->implicit_rotation_on_destroy = true;
0e270a1e
JG
3469 }
3470 } else if (session->has_been_started && session->current_trace_chunk) {
7fdbed1c
JG
3471 /*
3472 * The user has not triggered a session rotation. However, to
3473 * ensure all data has been consumed, the session is rotated
3474 * to a 'null' trace chunk before it is destroyed.
3475 *
3476 * This is a "quiet" rotation meaning that no notification is
3477 * emitted and no renaming of the current trace chunk takes
3478 * place.
3479 */
28ab034a 3480 ret = cmd_rotate_session(
cd9adb8b 3481 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
53fb6336
JG
3482 /*
3483 * Rotation operations may not be supported by the kernel
3484 * tracer. Hence, do not consider this implicit rotation as
3485 * a session destruction error. The library has already stopped
3486 * the session and waited for pending data; there is nothing
3487 * left to do but complete the destruction of the session.
3488 */
28ab034a 3489 if (ret != LTTNG_OK && ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
7fdbed1c 3490 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
28ab034a
JG
3491 session->name,
3492 lttng_strerror(ret));
7966af57 3493 destruction_last_error = (lttng_error_code) -ret;
7fdbed1c
JG
3494 }
3495 }
5c408ad8 3496
a503e1ef
JG
3497 if (session->shm_path[0]) {
3498 /*
3499 * When a session is created with an explicit shm_path,
3500 * the consumer daemon will create its shared memory files
3501 * at that location and will *not* unlink them. This is normal
3502 * as the intention of that feature is to make it possible
3503 * to retrieve the content of those files should a crash occur.
3504 *
3505 * To ensure the content of those files can be used, the
3506 * sessiond daemon will replicate the content of the metadata
3507 * cache in a metadata file.
3508 *
3509 * On clean-up, it is expected that the consumer daemon will
3510 * unlink the shared memory files and that the session daemon
3511 * will unlink the metadata file. Then, the session's directory
3512 * in the shm path can be removed.
3513 *
3514 * Unfortunately, a flaw in the design of the sessiond's and
3515 * consumerd's tear down of channels makes it impossible to
3516 * determine when the sessiond _and_ the consumerd have both
3517 * destroyed their representation of a channel. For one, the
3518 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3519 * callbacks in both daemons.
3520 *
3521 * However, it is also impossible for the sessiond to know when
3522 * the consumer daemon is done destroying its channel(s) since
3523 * it occurs as a reaction to the closing of the channel's file
3524 * descriptor. There is no resulting communication initiated
3525 * from the consumerd to the sessiond to confirm that the
3526 * operation is completed (and was successful).
3527 *
3528 * Until this is all fixed, the session daemon checks for the
3529 * removal of the session's shm path which makes it possible
3530 * to safely advertise a session as having been destroyed.
3531 *
3532 * Prior to this fix, it was not possible to reliably save
3533 * a session making use of the --shm-path option, destroy it,
3534 * and load it again. This is because the creation of the
3535 * session would fail upon seeing the session's shm path
3536 * already in existence.
3537 *
3538 * Note that none of the error paths in the check for the
3539 * directory's existence return an error. This is normal
3540 * as there isn't much that can be done. The session will
3541 * be destroyed properly, except that we can't offer the
3542 * guarantee that the same session can be re-created.
3543 */
3544 current_completion_handler = &destroy_completion_handler.handler;
3545 ret = lttng_strncpy(destroy_completion_handler.shm_path,
28ab034a
JG
3546 session->shm_path,
3547 sizeof(destroy_completion_handler.shm_path));
a0377dfe 3548 LTTNG_ASSERT(!ret);
a503e1ef 3549 }
e32d7f27
JG
3550
3551 /*
3552 * The session is destroyed. However, note that the command context
3553 * still holds a reference to the session, thus delaying its destruction
3554 * _at least_ up to the point when that reference is released.
3555 */
3556 session_destroy(session);
3e3665b8 3557 if (reply_context) {
3285a971 3558 reply_context->destruction_status = destruction_last_error;
28ab034a
JG
3559 ret = session_add_destroy_notifier(
3560 session, cmd_destroy_session_reply, (void *) reply_context);
3e3665b8
JG
3561 if (ret) {
3562 ret = LTTNG_ERR_FATAL;
3563 goto end;
3564 } else {
3565 *sock_fd = -1;
3566 }
0e270a1e
JG
3567 }
3568 ret = LTTNG_OK;
3e3665b8 3569end:
2f77fc4b
DG
3570 return ret;
3571}
3572
2f77fc4b
DG
3573/*
3574 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3575 */
56a37563 3576int cmd_register_consumer(struct ltt_session *session,
28ab034a
JG
3577 enum lttng_domain_type domain,
3578 const char *sock_path,
3579 struct consumer_data *cdata)
2f77fc4b
DG
3580{
3581 int ret, sock;
cd9adb8b 3582 struct consumer_socket *socket = nullptr;
2f77fc4b 3583
a0377dfe
FD
3584 LTTNG_ASSERT(session);
3585 LTTNG_ASSERT(cdata);
3586 LTTNG_ASSERT(sock_path);
2f77fc4b
DG
3587
3588 switch (domain) {
3589 case LTTNG_DOMAIN_KERNEL:
3590 {
3591 struct ltt_kernel_session *ksess = session->kernel_session;
3592
a0377dfe 3593 LTTNG_ASSERT(ksess);
2f77fc4b
DG
3594
3595 /* Can't register a consumer if there is already one */
3596 if (ksess->consumer_fds_sent != 0) {
f73fabfd 3597 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
3598 goto error;
3599 }
3600
3601 sock = lttcomm_connect_unix_sock(sock_path);
3602 if (sock < 0) {
f73fabfd 3603 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
3604 goto error;
3605 }
4ce514c4 3606 cdata->cmd_sock = sock;
2f77fc4b 3607
4ce514c4 3608 socket = consumer_allocate_socket(&cdata->cmd_sock);
cd9adb8b 3609 if (socket == nullptr) {
f66c074c
DG
3610 ret = close(sock);
3611 if (ret < 0) {
3612 PERROR("close register consumer");
3613 }
4ce514c4 3614 cdata->cmd_sock = -1;
f73fabfd 3615 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3616 goto error;
3617 }
3618
64803277 3619 socket->lock = zmalloc<pthread_mutex_t>();
cd9adb8b 3620 if (socket->lock == nullptr) {
2f77fc4b 3621 PERROR("zmalloc pthread mutex");
f73fabfd 3622 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3623 goto error;
3624 }
64803277 3625
cd9adb8b 3626 pthread_mutex_init(socket->lock, nullptr);
2f77fc4b
DG
3627 socket->registered = 1;
3628
56047f5a 3629 lttng::urcu::read_lock_guard read_lock;
2f77fc4b 3630 consumer_add_socket(socket, ksess->consumer);
2f77fc4b
DG
3631
3632 pthread_mutex_lock(&cdata->pid_mutex);
3633 cdata->pid = -1;
3634 pthread_mutex_unlock(&cdata->pid_mutex);
3635
3636 break;
3637 }
3638 default:
3639 /* TODO: Userspace tracing */
f73fabfd 3640 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3641 goto error;
3642 }
3643
dd81b457 3644 return LTTNG_OK;
2f77fc4b
DG
3645
3646error:
dd81b457
DG
3647 if (socket) {
3648 consumer_destroy_socket(socket);
3649 }
2f77fc4b
DG
3650 return ret;
3651}
3652
3653/*
3654 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3655 */
28ab034a 3656ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains)
2f77fc4b
DG
3657{
3658 int ret, index = 0;
3659 ssize_t nb_dom = 0;
fefd409b
DG
3660 struct agent *agt;
3661 struct lttng_ht_iter iter;
2f77fc4b 3662
cd9adb8b 3663 if (session->kernel_session != nullptr) {
2f77fc4b
DG
3664 DBG3("Listing domains found kernel domain");
3665 nb_dom++;
3666 }
3667
cd9adb8b 3668 if (session->ust_session != nullptr) {
2f77fc4b
DG
3669 DBG3("Listing domains found UST global domain");
3670 nb_dom++;
3c6a091f 3671
56047f5a
JG
3672 lttng::urcu::read_lock_guard read_lock;
3673
28ab034a
JG
3674 cds_lfht_for_each_entry (
3675 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
fefd409b
DG
3676 if (agt->being_used) {
3677 nb_dom++;
3678 }
3c6a091f 3679 }
2f77fc4b
DG
3680 }
3681
fa64dfb4
JG
3682 if (!nb_dom) {
3683 goto end;
3684 }
3685
64803277 3686 *domains = calloc<lttng_domain>(nb_dom);
cd9adb8b 3687 if (*domains == nullptr) {
f73fabfd 3688 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3689 goto error;
3690 }
3691
cd9adb8b 3692 if (session->kernel_session != nullptr) {
2f77fc4b 3693 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
b5edb9e8
PP
3694
3695 /* Kernel session buffer type is always GLOBAL */
3696 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3697
2f77fc4b
DG
3698 index++;
3699 }
3700
cd9adb8b 3701 if (session->ust_session != nullptr) {
2f77fc4b 3702 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 3703 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b 3704 index++;
3c6a091f 3705
56047f5a
JG
3706 {
3707 lttng::urcu::read_lock_guard read_lock;
3708
3709 cds_lfht_for_each_entry (
3710 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3711 if (agt->being_used) {
3712 (*domains)[index].type = agt->domain;
3713 (*domains)[index].buf_type =
3714 session->ust_session->buffer_type;
3715 index++;
3716 }
fefd409b 3717 }
3c6a091f 3718 }
2f77fc4b 3719 }
fa64dfb4 3720end:
2f77fc4b
DG
3721 return nb_dom;
3722
3723error:
f73fabfd
DG
3724 /* Return negative value to differentiate return code */
3725 return -ret;
2f77fc4b
DG
3726}
3727
2f77fc4b
DG
3728/*
3729 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3730 */
999af9c1 3731enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
28ab034a
JG
3732 struct ltt_session *session,
3733 struct lttng_payload *payload)
2f77fc4b 3734{
999af9c1
JR
3735 int ret = 0;
3736 unsigned int i = 0;
3737 struct lttcomm_list_command_header cmd_header = {};
3738 size_t cmd_header_offset;
3739 enum lttng_error_code ret_code;
3740
3741 assert(session);
3742 assert(payload);
3743
3744 DBG("Listing channels for session %s", session->name);
3745
3746 cmd_header_offset = payload->buffer.size;
3747
3748 /* Reserve space for command reply header. */
3749 ret = lttng_dynamic_buffer_set_size(&payload->buffer,
28ab034a 3750 cmd_header_offset + sizeof(cmd_header));
999af9c1
JR
3751 if (ret) {
3752 ret_code = LTTNG_ERR_NOMEM;
3753 goto end;
3754 }
2f77fc4b
DG
3755
3756 switch (domain) {
3757 case LTTNG_DOMAIN_KERNEL:
999af9c1
JR
3758 {
3759 /* Kernel channels */
3760 struct ltt_kernel_channel *kchan;
cd9adb8b 3761 if (session->kernel_session != nullptr) {
28ab034a
JG
3762 cds_list_for_each_entry (
3763 kchan, &session->kernel_session->channel_list.head, list) {
999af9c1
JR
3764 uint64_t discarded_events, lost_packets;
3765 struct lttng_channel_extended *extended;
3766
3767 extended = (struct lttng_channel_extended *)
28ab034a 3768 kchan->channel->attr.extended.ptr;
999af9c1 3769
28ab034a
JG
3770 ret = get_kernel_runtime_stats(
3771 session, kchan, &discarded_events, &lost_packets);
999af9c1
JR
3772 if (ret < 0) {
3773 ret_code = LTTNG_ERR_UNK;
3774 goto end;
3775 }
3776
3777 /*
3778 * Update the discarded_events and lost_packets
3779 * count for the channel
3780 */
3781 extended->discarded_events = discarded_events;
3782 extended->lost_packets = lost_packets;
3783
28ab034a 3784 ret = lttng_channel_serialize(kchan->channel, &payload->buffer);
999af9c1
JR
3785 if (ret) {
3786 ERR("Failed to serialize lttng_channel: channel name = '%s'",
28ab034a 3787 kchan->channel->name);
999af9c1
JR
3788 ret_code = LTTNG_ERR_UNK;
3789 goto end;
3790 }
3791
3792 i++;
3793 }
c7d620a2 3794 }
2f77fc4b 3795 break;
999af9c1 3796 }
2f77fc4b 3797 case LTTNG_DOMAIN_UST:
999af9c1
JR
3798 {
3799 struct lttng_ht_iter iter;
3800 struct ltt_ust_channel *uchan;
3801
56047f5a
JG
3802 {
3803 lttng::urcu::read_lock_guard read_lock;
3804
3805 cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht,
3806 &iter.iter,
3807 uchan,
3808 node.node) {
3809 uint64_t discarded_events = 0, lost_packets = 0;
3810 struct lttng_channel *channel = nullptr;
3811 struct lttng_channel_extended *extended;
999af9c1 3812
56047f5a
JG
3813 channel = trace_ust_channel_to_lttng_channel(uchan);
3814 if (!channel) {
3815 ret_code = LTTNG_ERR_NOMEM;
3816 goto end;
3817 }
999af9c1 3818
56047f5a
JG
3819 extended = (struct lttng_channel_extended *)
3820 channel->attr.extended.ptr;
999af9c1 3821
56047f5a
JG
3822 ret = get_ust_runtime_stats(
3823 session, uchan, &discarded_events, &lost_packets);
3824 if (ret < 0) {
3825 lttng_channel_destroy(channel);
3826 ret_code = LTTNG_ERR_UNK;
3827 goto end;
3828 }
3829
3830 extended->discarded_events = discarded_events;
3831 extended->lost_packets = lost_packets;
3832
3833 ret = lttng_channel_serialize(channel, &payload->buffer);
3834 if (ret) {
3835 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3836 channel->name);
3837 lttng_channel_destroy(channel);
3838 ret_code = LTTNG_ERR_UNK;
3839 goto end;
3840 }
999af9c1 3841
ae2275af 3842 lttng_channel_destroy(channel);
56047f5a 3843 i++;
999af9c1 3844 }
c7d620a2 3845 }
56047f5a 3846
2f77fc4b 3847 break;
999af9c1 3848 }
2f77fc4b 3849 default:
999af9c1 3850 break;
2f77fc4b
DG
3851 }
3852
999af9c1
JR
3853 if (i > UINT32_MAX) {
3854 ERR("Channel count would overflow the channel listing command's reply");
3855 ret_code = LTTNG_ERR_OVERFLOW;
3856 goto end;
2f77fc4b
DG
3857 }
3858
999af9c1
JR
3859 /* Update command reply header. */
3860 cmd_header.count = (uint32_t) i;
28ab034a 3861 memcpy(payload->buffer.data + cmd_header_offset, &cmd_header, sizeof(cmd_header));
999af9c1
JR
3862 ret_code = LTTNG_OK;
3863
53e367f9 3864end:
999af9c1 3865 return ret_code;
2f77fc4b
DG
3866}
3867
3868/*
3869 * Command LTTNG_LIST_EVENTS processed by the client thread.
3870 */
8ddd72ef 3871enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
28ab034a
JG
3872 struct ltt_session *session,
3873 char *channel_name,
3874 struct lttng_payload *reply_payload)
2f77fc4b 3875{
8ddd72ef
JR
3876 int buffer_resize_ret;
3877 enum lttng_error_code ret_code = LTTNG_OK;
3878 struct lttcomm_list_command_header reply_command_header = {};
3879 size_t reply_command_header_offset;
23831239 3880 unsigned int nb_events = 0;
e368fb43 3881
8ddd72ef
JR
3882 assert(reply_payload);
3883
3884 /* Reserve space for command reply header. */
3885 reply_command_header_offset = reply_payload->buffer.size;
28ab034a
JG
3886 buffer_resize_ret = lttng_dynamic_buffer_set_size(
3887 &reply_payload->buffer,
3888 reply_command_header_offset + sizeof(struct lttcomm_list_command_header));
8ddd72ef
JR
3889 if (buffer_resize_ret) {
3890 ret_code = LTTNG_ERR_NOMEM;
3891 goto end;
e368fb43 3892 }
2f77fc4b
DG
3893
3894 switch (domain) {
3895 case LTTNG_DOMAIN_KERNEL:
cd9adb8b 3896 if (session->kernel_session != nullptr) {
28ab034a
JG
3897 ret_code = list_lttng_kernel_events(
3898 channel_name, session->kernel_session, reply_payload, &nb_events);
2f77fc4b 3899 }
8ddd72ef 3900
2f77fc4b
DG
3901 break;
3902 case LTTNG_DOMAIN_UST:
3903 {
cd9adb8b 3904 if (session->ust_session != nullptr) {
28ab034a
JG
3905 ret_code =
3906 list_lttng_ust_global_events(channel_name,
3907 &session->ust_session->domain_global,
3908 reply_payload,
3909 &nb_events);
2f77fc4b 3910 }
8ddd72ef 3911
2f77fc4b
DG
3912 break;
3913 }
5cdb6027 3914 case LTTNG_DOMAIN_LOG4J:
3c6a091f 3915 case LTTNG_DOMAIN_JUL:
0e115563 3916 case LTTNG_DOMAIN_PYTHON:
3c6a091f 3917 if (session->ust_session) {
fefd409b
DG
3918 struct lttng_ht_iter iter;
3919 struct agent *agt;
3920
56047f5a
JG
3921 lttng::urcu::read_lock_guard read_lock;
3922
28ab034a
JG
3923 cds_lfht_for_each_entry (
3924 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
1dfd9906 3925 if (agt->domain == domain) {
8ddd72ef 3926 ret_code = list_lttng_agent_events(
28ab034a 3927 agt, reply_payload, &nb_events);
1dfd9906
JG
3928 break;
3929 }
fefd409b 3930 }
3c6a091f
DG
3931 }
3932 break;
2f77fc4b 3933 default:
8ddd72ef
JR
3934 ret_code = LTTNG_ERR_UND;
3935 break;
2f77fc4b
DG
3936 }
3937
8ddd72ef
JR
3938 if (nb_events > UINT32_MAX) {
3939 ret_code = LTTNG_ERR_OVERFLOW;
3940 goto end;
3941 }
e368fb43 3942
8ddd72ef
JR
3943 /* Update command reply header. */
3944 reply_command_header.count = (uint32_t) nb_events;
28ab034a
JG
3945 memcpy(reply_payload->buffer.data + reply_command_header_offset,
3946 &reply_command_header,
3947 sizeof(reply_command_header));
2f77fc4b 3948
8ddd72ef
JR
3949end:
3950 return ret_code;
2f77fc4b
DG
3951}
3952
3953/*
3954 * Using the session list, filled a lttng_session array to send back to the
3955 * client for session listing.
3956 *
3957 * The session list lock MUST be acquired before calling this function. Use
3958 * session_lock_list() and session_unlock_list().
3959 */
b178f53e 3960void cmd_list_lttng_sessions(struct lttng_session *sessions,
28ab034a
JG
3961 size_t session_count,
3962 uid_t uid,
3963 gid_t gid)
2f77fc4b
DG
3964{
3965 int ret;
3966 unsigned int i = 0;
3967 struct ltt_session *session;
3968 struct ltt_session_list *list = session_get_list();
28ab034a 3969 struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]);
2f77fc4b 3970
28ab034a 3971 DBG("Getting all available session for UID %d GID %d", uid, gid);
2f77fc4b
DG
3972 /*
3973 * Iterate over session list and append data after the control struct in
3974 * the buffer.
3975 */
28ab034a 3976 cds_list_for_each_entry (session, &list->head, list) {
e32d7f27
JG
3977 if (!session_get(session)) {
3978 continue;
3979 }
2f77fc4b
DG
3980 /*
3981 * Only list the sessions the user can control.
3982 */
28ab034a 3983 if (!session_access_ok(session, uid) || session->destroyed) {
e32d7f27 3984 session_put(session);
2f77fc4b
DG
3985 continue;
3986 }
3987
3988 struct ltt_kernel_session *ksess = session->kernel_session;
3989 struct ltt_ust_session *usess = session->ust_session;
3990
3991 if (session->consumer->type == CONSUMER_DST_NET ||
28ab034a
JG
3992 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3993 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3994 ret = build_network_session_path(
3995 sessions[i].path, sizeof(sessions[i].path), session);
2f77fc4b 3996 } else {
28ab034a
JG
3997 ret = snprintf(sessions[i].path,
3998 sizeof(sessions[i].path),
3999 "%s",
4000 session->consumer->dst.session_root_path);
2f77fc4b
DG
4001 }
4002 if (ret < 0) {
4003 PERROR("snprintf session path");
e32d7f27 4004 session_put(session);
2f77fc4b
DG
4005 continue;
4006 }
4007
4008 strncpy(sessions[i].name, session->name, NAME_MAX);
4009 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 4010 sessions[i].enabled = session->active;
2cbf8fed 4011 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 4012 sessions[i].live_timer_interval = session->live_timer;
b178f53e
JG
4013 extended[i].creation_time.value = (uint64_t) session->creation_time;
4014 extended[i].creation_time.is_set = 1;
2f77fc4b 4015 i++;
e32d7f27 4016 session_put(session);
2f77fc4b
DG
4017 }
4018}
4019
806e2684 4020/*
6d805429 4021 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 4022 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 4023 */
6d805429 4024int cmd_data_pending(struct ltt_session *session)
806e2684
DG
4025{
4026 int ret;
4027 struct ltt_kernel_session *ksess = session->kernel_session;
4028 struct ltt_ust_session *usess = session->ust_session;
4029
a0377dfe 4030 LTTNG_ASSERT(session);
806e2684 4031
5c408ad8
JD
4032 DBG("Data pending for session %s", session->name);
4033
806e2684 4034 /* Session MUST be stopped to ask for data availability. */
8382cf6f 4035 if (session->active) {
806e2684
DG
4036 ret = LTTNG_ERR_SESSION_STARTED;
4037 goto error;
3a89d11a
DG
4038 } else {
4039 /*
4040 * If stopped, just make sure we've started before else the above call
4041 * will always send that there is data pending.
4042 *
4043 * The consumer assumes that when the data pending command is received,
4044 * the trace has been started before or else no output data is written
4045 * by the streams which is a condition for data pending. So, this is
4046 * *VERY* important that we don't ask the consumer before a start
4047 * trace.
4048 */
8382cf6f 4049 if (!session->has_been_started) {
3a89d11a
DG
4050 ret = 0;
4051 goto error;
4052 }
806e2684
DG
4053 }
4054
92816cc3
JG
4055 /* A rotation is still pending, we have to wait. */
4056 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5c408ad8
JD
4057 DBG("Rotate still pending for session %s", session->name);
4058 ret = 1;
4059 goto error;
4060 }
4061
806e2684 4062 if (ksess && ksess->consumer) {
6d805429
DG
4063 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
4064 if (ret == 1) {
806e2684
DG
4065 /* Data is still being extracted for the kernel. */
4066 goto error;
4067 }
4068 }
4069
4070 if (usess && usess->consumer) {
6d805429
DG
4071 ret = consumer_is_data_pending(usess->id, usess->consumer);
4072 if (ret == 1) {
806e2684
DG
4073 /* Data is still being extracted for the kernel. */
4074 goto error;
4075 }
4076 }
4077
4078 /* Data is ready to be read by a viewer */
6d805429 4079 ret = 0;
806e2684
DG
4080
4081error:
4082 return ret;
4083}
4084
6dc3064a
DG
4085/*
4086 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4087 *
4088 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4089 */
4090int cmd_snapshot_add_output(struct ltt_session *session,
28ab034a
JG
4091 const struct lttng_snapshot_output *output,
4092 uint32_t *id)
6dc3064a
DG
4093{
4094 int ret;
4095 struct snapshot_output *new_output;
4096
a0377dfe
FD
4097 LTTNG_ASSERT(session);
4098 LTTNG_ASSERT(output);
6dc3064a
DG
4099
4100 DBG("Cmd snapshot add output for session %s", session->name);
4101
4102 /*
903ef685 4103 * Can't create an output if the session is not set in no-output mode.
6dc3064a
DG
4104 */
4105 if (session->output_traces) {
903ef685 4106 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
4107 goto error;
4108 }
4109
54213acc
JG
4110 if (session->has_non_mmap_channel) {
4111 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
4112 goto error;
4113 }
4114
6dc3064a
DG
4115 /* Only one output is allowed until we have the "tee" feature. */
4116 if (session->snapshot.nb_output == 1) {
4117 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
4118 goto error;
4119 }
4120
4121 new_output = snapshot_output_alloc();
4122 if (!new_output) {
4123 ret = LTTNG_ERR_NOMEM;
4124 goto error;
4125 }
4126
28ab034a
JG
4127 ret = snapshot_output_init(session,
4128 output->max_size,
4129 output->name,
4130 output->ctrl_url,
4131 output->data_url,
4132 session->consumer,
4133 new_output,
4134 &session->snapshot);
6dc3064a
DG
4135 if (ret < 0) {
4136 if (ret == -ENOMEM) {
4137 ret = LTTNG_ERR_NOMEM;
4138 } else {
4139 ret = LTTNG_ERR_INVALID;
4140 }
4141 goto free_error;
4142 }
4143
6dc3064a
DG
4144 snapshot_add_output(&session->snapshot, new_output);
4145 if (id) {
4146 *id = new_output->id;
4147 }
6dc3064a
DG
4148
4149 return LTTNG_OK;
4150
4151free_error:
4152 snapshot_output_destroy(new_output);
4153error:
4154 return ret;
4155}
4156
4157/*
4158 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4159 *
4160 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4161 */
28ab034a 4162int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output)
6dc3064a
DG
4163{
4164 int ret;
cd9adb8b 4165 struct snapshot_output *sout = nullptr;
6dc3064a 4166
a0377dfe
FD
4167 LTTNG_ASSERT(session);
4168 LTTNG_ASSERT(output);
6dc3064a 4169
56047f5a 4170 lttng::urcu::read_lock_guard read_lock;
6dc3064a
DG
4171
4172 /*
d3f14b8a
MD
4173 * Permission denied to create an output if the session is not
4174 * set in no output mode.
6dc3064a
DG
4175 */
4176 if (session->output_traces) {
903ef685 4177 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
4178 goto error;
4179 }
4180
eb240553 4181 if (output->id) {
28ab034a
JG
4182 DBG("Cmd snapshot del output id %" PRIu32 " for session %s",
4183 output->id,
4184 session->name);
eb240553
DG
4185 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
4186 } else if (*output->name != '\0') {
28ab034a 4187 DBG("Cmd snapshot del output name %s for session %s", output->name, session->name);
eb240553
DG
4188 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
4189 }
6dc3064a
DG
4190 if (!sout) {
4191 ret = LTTNG_ERR_INVALID;
4192 goto error;
4193 }
4194
4195 snapshot_delete_output(&session->snapshot, sout);
4196 snapshot_output_destroy(sout);
4197 ret = LTTNG_OK;
4198
4199error:
6dc3064a
DG
4200 return ret;
4201}
4202
4203/*
4204 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4205 *
4206 * If no output is available, outputs is untouched and 0 is returned.
4207 *
4208 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4209 */
4210ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
28ab034a 4211 struct lttng_snapshot_output **outputs)
6dc3064a
DG
4212{
4213 int ret, idx = 0;
cd9adb8b 4214 struct lttng_snapshot_output *list = nullptr;
6dc3064a
DG
4215 struct lttng_ht_iter iter;
4216 struct snapshot_output *output;
4217
a0377dfe
FD
4218 LTTNG_ASSERT(session);
4219 LTTNG_ASSERT(outputs);
6dc3064a
DG
4220
4221 DBG("Cmd snapshot list outputs for session %s", session->name);
4222
4223 /*
d3f14b8a
MD
4224 * Permission denied to create an output if the session is not
4225 * set in no output mode.
6dc3064a
DG
4226 */
4227 if (session->output_traces) {
903ef685
JG
4228 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4229 goto end;
6dc3064a
DG
4230 }
4231
4232 if (session->snapshot.nb_output == 0) {
4233 ret = 0;
903ef685 4234 goto end;
6dc3064a
DG
4235 }
4236
64803277 4237 list = calloc<lttng_snapshot_output>(session->snapshot.nb_output);
6dc3064a 4238 if (!list) {
b223ca94 4239 ret = -LTTNG_ERR_NOMEM;
903ef685 4240 goto end;
6dc3064a
DG
4241 }
4242
4243 /* Copy list from session to the new list object. */
56047f5a
JG
4244 {
4245 lttng::urcu::read_lock_guard read_lock;
4246
4247 cds_lfht_for_each_entry (
4248 session->snapshot.output_ht->ht, &iter.iter, output, node.node) {
4249 LTTNG_ASSERT(output->consumer);
4250 list[idx].id = output->id;
4251 list[idx].max_size = output->max_size;
4252 if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) {
6ce22875 4253 ret = -LTTNG_ERR_INVALID;
903ef685 4254 goto error;
6ce22875 4255 }
6dc3064a 4256
56047f5a
JG
4257 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4258 if (lttng_strncpy(list[idx].ctrl_url,
4259 output->consumer->dst.session_root_path,
4260 sizeof(list[idx].ctrl_url))) {
4261 ret = -LTTNG_ERR_INVALID;
4262 goto error;
4263 }
4264 } else {
4265 /* Control URI. */
4266 ret = uri_to_str_url(&output->consumer->dst.net.control,
4267 list[idx].ctrl_url,
4268 sizeof(list[idx].ctrl_url));
4269 if (ret < 0) {
4270 ret = -LTTNG_ERR_NOMEM;
4271 goto error;
4272 }
4273
4274 /* Data URI. */
4275 ret = uri_to_str_url(&output->consumer->dst.net.data,
4276 list[idx].data_url,
4277 sizeof(list[idx].data_url));
4278 if (ret < 0) {
4279 ret = -LTTNG_ERR_NOMEM;
4280 goto error;
4281 }
6dc3064a 4282 }
56047f5a
JG
4283
4284 idx++;
6dc3064a 4285 }
6dc3064a
DG
4286 }
4287
4288 *outputs = list;
cd9adb8b 4289 list = nullptr;
b223ca94 4290 ret = session->snapshot.nb_output;
6dc3064a 4291error:
b223ca94 4292 free(list);
903ef685 4293end:
b223ca94 4294 return ret;
6dc3064a
DG
4295}
4296
93ec662e
JD
4297/*
4298 * Check if we can regenerate the metadata for this session.
4299 * Only kernel, UST per-uid and non-live sessions are supported.
4300 *
4301 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4302 */
28ab034a 4303static int check_regenerate_metadata_support(struct ltt_session *session)
93ec662e
JD
4304{
4305 int ret;
4306
a0377dfe 4307 LTTNG_ASSERT(session);
93ec662e
JD
4308
4309 if (session->live_timer != 0) {
4310 ret = LTTNG_ERR_LIVE_SESSION;
4311 goto end;
4312 }
4313 if (!session->active) {
4314 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4315 goto end;
4316 }
4317 if (session->ust_session) {
4318 switch (session->ust_session->buffer_type) {
4319 case LTTNG_BUFFER_PER_UID:
4320 break;
4321 case LTTNG_BUFFER_PER_PID:
4322 ret = LTTNG_ERR_PER_PID_SESSION;
4323 goto end;
4324 default:
a0377dfe 4325 abort();
93ec662e
JD
4326 ret = LTTNG_ERR_UNK;
4327 goto end;
4328 }
4329 }
4330 if (session->consumer->type == CONSUMER_DST_NET &&
28ab034a 4331 session->consumer->relay_minor_version < 8) {
93ec662e
JD
4332 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4333 goto end;
4334 }
4335 ret = 0;
4336
4337end:
4338 return ret;
4339}
4340
93ec662e 4341/*
eded6438 4342 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
93ec662e
JD
4343 *
4344 * Ask the consumer to truncate the existing metadata file(s) and
4345 * then regenerate the metadata. Live and per-pid sessions are not
4346 * supported and return an error.
4347 *
1136f41b 4348 * Return LTTNG_OK on success or else a LTTNG_ERR code.
93ec662e 4349 */
eded6438 4350int cmd_regenerate_metadata(struct ltt_session *session)
93ec662e
JD
4351{
4352 int ret;
4353
a0377dfe 4354 LTTNG_ASSERT(session);
93ec662e 4355
eded6438 4356 ret = check_regenerate_metadata_support(session);
93ec662e
JD
4357 if (ret) {
4358 goto end;
4359 }
4360
4361 if (session->kernel_session) {
28ab034a 4362 ret = kernctl_session_regenerate_metadata(session->kernel_session->fd);
93ec662e
JD
4363 if (ret < 0) {
4364 ERR("Failed to regenerate the kernel metadata");
4365 goto end;
4366 }
4367 }
4368
4369 if (session->ust_session) {
d7bfb9b0 4370 ret = trace_ust_regenerate_metadata(session->ust_session);
93ec662e
JD
4371 if (ret < 0) {
4372 ERR("Failed to regenerate the UST metadata");
4373 goto end;
4374 }
4375 }
4376 DBG("Cmd metadata regenerate for session %s", session->name);
4377 ret = LTTNG_OK;
4378
4379end:
4380 return ret;
4381}
4382
c2561365
JD
4383/*
4384 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4385 *
4386 * Ask the tracer to regenerate a new statedump.
4387 *
1136f41b 4388 * Return LTTNG_OK on success or else a LTTNG_ERR code.
c2561365
JD
4389 */
4390int cmd_regenerate_statedump(struct ltt_session *session)
4391{
4392 int ret;
4393
a0377dfe 4394 LTTNG_ASSERT(session);
c2561365
JD
4395
4396 if (!session->active) {
4397 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4398 goto end;
4399 }
c2561365
JD
4400
4401 if (session->kernel_session) {
28ab034a 4402 ret = kernctl_session_regenerate_statedump(session->kernel_session->fd);
c2561365
JD
4403 /*
4404 * Currently, the statedump in kernel can only fail if out
4405 * of memory.
4406 */
4407 if (ret < 0) {
4408 if (ret == -ENOMEM) {
4409 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4410 } else {
4411 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4412 }
4413 ERR("Failed to regenerate the kernel statedump");
4414 goto end;
4415 }
4416 }
4417
4418 if (session->ust_session) {
4419 ret = ust_app_regenerate_statedump_all(session->ust_session);
4420 /*
4421 * Currently, the statedump in UST always returns 0.
4422 */
4423 if (ret < 0) {
4424 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4425 ERR("Failed to regenerate the UST statedump");
4426 goto end;
4427 }
4428 }
4429 DBG("Cmd regenerate statedump for session %s", session->name);
4430 ret = LTTNG_OK;
4431
4432end:
4433 return ret;
4434}
4435
28ab034a
JG
4436static enum lttng_error_code
4437synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread,
4438 struct lttng_trigger *trigger,
4439 const struct lttng_credentials *cmd_creds)
70670472 4440{
989a0844 4441 enum lttng_error_code ret_code;
28ab034a 4442 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
989a0844
FD
4443 const char *trigger_name;
4444 uid_t trigger_owner;
4445 enum lttng_trigger_status trigger_status;
4446 const enum lttng_domain_type trigger_domain =
28ab034a 4447 lttng_trigger_get_underlying_domain_type_restriction(trigger);
70670472 4448
989a0844 4449 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4450 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
989a0844 4451
a0377dfe
FD
4452 LTTNG_ASSERT(condition);
4453 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
28ab034a 4454 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
989a0844
FD
4455
4456 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
28ab034a 4457 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
989a0844
FD
4458
4459 session_lock_list();
4460 switch (trigger_domain) {
4461 case LTTNG_DOMAIN_KERNEL:
4462 {
4463 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4464 if (ret_code != LTTNG_OK) {
4465 enum lttng_error_code notif_thread_unregister_ret;
4466
4467 notif_thread_unregister_ret =
28ab034a
JG
4468 notification_thread_command_unregister_trigger(notification_thread,
4469 trigger);
989a0844
FD
4470
4471 if (notif_thread_unregister_ret != LTTNG_OK) {
4472 /* Return the original error code. */
4473 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4474 trigger_name,
4475 (int) trigger_owner,
4476 ret_code);
989a0844 4477 }
7ebbf3f0
JG
4478
4479 goto end_unlock_session_list;
989a0844
FD
4480 }
4481 break;
70670472 4482 }
989a0844
FD
4483 case LTTNG_DOMAIN_UST:
4484 ust_app_global_update_all_event_notifier_rules();
4485 break;
4486 case LTTNG_DOMAIN_JUL:
4487 case LTTNG_DOMAIN_LOG4J:
4488 case LTTNG_DOMAIN_PYTHON:
4489 {
4490 /* Agent domains. */
28ab034a 4491 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
70670472 4492
989a0844
FD
4493 if (!agt) {
4494 agt = agent_create(trigger_domain);
4495 if (!agt) {
4496 ret_code = LTTNG_ERR_NOMEM;
4497 goto end_unlock_session_list;
4498 }
4499
412d7227 4500 agent_add(agt, the_trigger_agents_ht_by_domain);
989a0844
FD
4501 }
4502
7966af57 4503 ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
989a0844
FD
4504 if (ret_code != LTTNG_OK) {
4505 goto end_unlock_session_list;
4506 }
4507
4508 break;
4509 }
4510 case LTTNG_DOMAIN_NONE:
4511 default:
4512 abort();
4513 }
4514
4515 ret_code = LTTNG_OK;
4516end_unlock_session_list:
4517 session_unlock_list();
70670472
JR
4518 return ret_code;
4519}
4520
4521enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
28ab034a
JG
4522 struct lttng_trigger *trigger,
4523 bool is_trigger_anonymous,
4524 struct notification_thread_handle *notification_thread,
4525 struct lttng_trigger **return_trigger)
b0880ae5 4526{
70670472 4527 enum lttng_error_code ret_code;
70670472
JR
4528 const char *trigger_name;
4529 uid_t trigger_owner;
4530 enum lttng_trigger_status trigger_status;
4531
4532 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
28ab034a 4533 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
ce0b1d61 4534
28ab034a 4535 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4536 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
70670472
JR
4537
4538 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4539 trigger_name,
4540 (int) trigger_owner,
4541 (int) lttng_credentials_get_uid(cmd_creds));
b0880ae5 4542
64eafdf6
JR
4543 /*
4544 * Validate the trigger credentials against the command credentials.
4545 * Only the root user can register a trigger with non-matching
4546 * credentials.
4547 */
28ab034a 4548 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
746e08d7 4549 if (lttng_credentials_get_uid(cmd_creds) != 0) {
70670472 4550 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4551 trigger_name,
4552 (int) trigger_owner,
4553 (int) lttng_credentials_get_uid(cmd_creds));
70670472 4554 ret_code = LTTNG_ERR_INVALID_TRIGGER;
64eafdf6
JR
4555 goto end;
4556 }
4557 }
3da864a9 4558
58daac01
JR
4559 /*
4560 * The bytecode generation also serves as a validation step for the
4561 * bytecode expressions.
4562 */
70670472
JR
4563 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4564 if (ret_code != LTTNG_OK) {
4565 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4566 trigger_name,
4567 (int) trigger_owner,
4568 ret_code);
58daac01
JR
4569 goto end;
4570 }
4571
242388e4
JR
4572 /*
4573 * A reference to the trigger is acquired by the notification thread.
4574 * It is safe to return the same trigger to the caller since it the
4575 * other user holds a reference.
4576 *
4577 * The trigger is modified during the execution of the
4578 * "register trigger" command. However, by the time the command returns,
4579 * it is safe to use without any locking as its properties are
4580 * immutable.
4581 */
0efb2ad7 4582 ret_code = notification_thread_command_register_trigger(
28ab034a 4583 notification_thread, trigger, is_trigger_anonymous);
70670472 4584 if (ret_code != LTTNG_OK) {
ce0b1d61 4585 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4586 trigger_name,
4587 (int) trigger_owner,
4588 ret_code);
44760c20 4589 goto end;
70670472
JR
4590 }
4591
ce0b1d61 4592 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
28ab034a 4593 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
ce0b1d61 4594
70670472
JR
4595 /*
4596 * Synchronize tracers if the trigger adds an event notifier.
4597 */
989a0844 4598 if (lttng_trigger_needs_tracer_notifier(trigger)) {
28ab034a
JG
4599 ret_code = synchronize_tracer_notifier_register(
4600 notification_thread, trigger, cmd_creds);
989a0844 4601 if (ret_code != LTTNG_OK) {
28ab034a 4602 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
989a0844 4603 goto end;
70670472
JR
4604 }
4605 }
4606
746e08d7
JG
4607 /*
4608 * Return an updated trigger to the client.
4609 *
4610 * Since a modified version of the same trigger is returned, acquire a
4611 * reference to the trigger so the caller doesn't have to care if those
4612 * are distinct instances or not.
4613 */
39b95a70
JG
4614 if (ret_code == LTTNG_OK) {
4615 lttng_trigger_get(trigger);
4616 *return_trigger = trigger;
4617 /* Ownership of trigger was transferred to caller. */
cd9adb8b 4618 trigger = nullptr;
39b95a70 4619 }
b0880ae5 4620end:
70670472 4621 return ret_code;
989a0844
FD
4622}
4623
28ab034a
JG
4624static enum lttng_error_code
4625synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
989a0844
FD
4626{
4627 enum lttng_error_code ret_code;
28ab034a 4628 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
989a0844 4629 const enum lttng_domain_type trigger_domain =
28ab034a 4630 lttng_trigger_get_underlying_domain_type_restriction(trigger);
989a0844 4631
a0377dfe
FD
4632 LTTNG_ASSERT(condition);
4633 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
28ab034a 4634 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
989a0844
FD
4635
4636 session_lock_list();
4637 switch (trigger_domain) {
4638 case LTTNG_DOMAIN_KERNEL:
4639 ret_code = kernel_unregister_event_notifier(trigger);
e689039f
JG
4640 if (ret_code != LTTNG_OK) {
4641 goto end_unlock_session_list;
4642 }
4643
989a0844
FD
4644 break;
4645 case LTTNG_DOMAIN_UST:
4646 ust_app_global_update_all_event_notifier_rules();
4647 break;
4648 case LTTNG_DOMAIN_JUL:
4649 case LTTNG_DOMAIN_LOG4J:
4650 case LTTNG_DOMAIN_PYTHON:
4651 {
4652 /* Agent domains. */
28ab034a 4653 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
989a0844 4654
566190c4
JG
4655 /*
4656 * This trigger was never registered in the first place. Calling
4657 * this function under those circumstances is an internal error.
4658 */
a0377dfe 4659 LTTNG_ASSERT(agt);
7966af57 4660 ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
989a0844
FD
4661 if (ret_code != LTTNG_OK) {
4662 goto end_unlock_session_list;
4663 }
4664
4665 break;
4666 }
4667 case LTTNG_DOMAIN_NONE:
4668 default:
4669 abort();
4670 }
4671
4672 ret_code = LTTNG_OK;
4673
9b7cbebd
JG
4674end_unlock_session_list:
4675 session_unlock_list();
4676 return ret_code;
b0880ae5
JG
4677}
4678
70670472 4679enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
28ab034a
JG
4680 const struct lttng_trigger *trigger,
4681 struct notification_thread_handle *notification_thread)
b0880ae5 4682{
70670472 4683 enum lttng_error_code ret_code;
70670472
JR
4684 const char *trigger_name;
4685 uid_t trigger_owner;
4686 enum lttng_trigger_status trigger_status;
cd9adb8b 4687 struct lttng_trigger *sessiond_trigger = nullptr;
70670472
JR
4688
4689 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
0efb2ad7 4690 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
989a0844 4691 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4692 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
70670472
JR
4693
4694 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4695 trigger_name,
4696 (int) trigger_owner,
4697 (int) lttng_credentials_get_uid(cmd_creds));
b0880ae5 4698
64eafdf6
JR
4699 /*
4700 * Validate the trigger credentials against the command credentials.
4701 * Only the root user can unregister a trigger with non-matching
4702 * credentials.
4703 */
28ab034a 4704 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
746e08d7 4705 if (lttng_credentials_get_uid(cmd_creds) != 0) {
70670472 4706 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4707 trigger_name,
4708 (int) trigger_owner,
4709 (int) lttng_credentials_get_uid(cmd_creds));
70670472 4710 ret_code = LTTNG_ERR_INVALID_TRIGGER;
64eafdf6
JR
4711 goto end;
4712 }
4713 }
3da864a9 4714
5c5373c3
JR
4715 /* Fetch the sessiond side trigger object. */
4716 ret_code = notification_thread_command_get_trigger(
28ab034a 4717 notification_thread, trigger, &sessiond_trigger);
5c5373c3
JR
4718 if (ret_code != LTTNG_OK) {
4719 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4720 trigger_name,
4721 (int) trigger_owner,
4722 ret_code);
5c5373c3
JR
4723 goto end;
4724 }
4725
a0377dfe 4726 LTTNG_ASSERT(sessiond_trigger);
5c5373c3
JR
4727
4728 /*
4729 * From this point on, no matter what, consider the trigger
4730 * unregistered.
4731 *
4732 * We set the unregistered state of the sessiond side trigger object in
4733 * the client thread since we want to minimize the possibility of the
4734 * notification thread being stalled due to a long execution of an
4735 * action that required the trigger lock.
4736 */
4737 lttng_trigger_set_as_unregistered(sessiond_trigger);
4738
28ab034a 4739 ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger);
70670472 4740 if (ret_code != LTTNG_OK) {
ce0b1d61 4741 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
28ab034a
JG
4742 trigger_name,
4743 (int) trigger_owner,
4744 ret_code);
13839b27 4745 goto end;
70670472
JR
4746 }
4747
4748 /*
4749 * Synchronize tracers if the trigger removes an event notifier.
44760c20
JR
4750 * Do this even if the trigger unregistration failed to at least stop
4751 * the tracers from producing notifications associated with this
4752 * event notifier.
70670472 4753 */
989a0844
FD
4754 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4755 ret_code = synchronize_tracer_notifier_unregister(trigger);
4756 if (ret_code != LTTNG_OK) {
4757 ERR("Error unregistering trigger to tracer.");
4758 goto end;
70670472
JR
4759 }
4760 }
4761
b0880ae5 4762end:
5c5373c3 4763 lttng_trigger_put(sessiond_trigger);
70670472 4764 return ret_code;
989a0844 4765}
b0880ae5 4766
ddd915a3 4767enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
28ab034a
JG
4768 struct notification_thread_handle *notification_thread,
4769 struct lttng_triggers **return_triggers)
fbc9f37d 4770{
f2bda80e 4771 int ret;
fbc9f37d 4772 enum lttng_error_code ret_code;
cd9adb8b 4773 struct lttng_triggers *triggers = nullptr;
fbc9f37d
JR
4774
4775 /* Get the set of triggers from the notification thread. */
4776 ret_code = notification_thread_command_list_triggers(
28ab034a 4777 notification_thread, cmd_ctx->creds.uid, &triggers);
fbc9f37d 4778 if (ret_code != LTTNG_OK) {
fbc9f37d
JR
4779 goto end;
4780 }
4781
f2bda80e
JG
4782 ret = lttng_triggers_remove_hidden_triggers(triggers);
4783 if (ret) {
4784 ret_code = LTTNG_ERR_UNK;
4785 goto end;
4786 }
4787
fbc9f37d 4788 *return_triggers = triggers;
cd9adb8b 4789 triggers = nullptr;
ddd915a3 4790 ret_code = LTTNG_OK;
fbc9f37d
JR
4791end:
4792 lttng_triggers_destroy(triggers);
ddd915a3 4793 return ret_code;
fbc9f37d 4794}
588c4b0d 4795
28ab034a
JG
4796enum lttng_error_code
4797cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4798 const struct lttng_error_query *query,
4799 struct lttng_error_query_results **_results,
4800 struct notification_thread_handle *notification_thread)
588c4b0d
JG
4801{
4802 enum lttng_error_code ret_code;
4803 const struct lttng_trigger *query_target_trigger;
cd9adb8b
JG
4804 const struct lttng_action *query_target_action = nullptr;
4805 struct lttng_trigger *matching_trigger = nullptr;
588c4b0d
JG
4806 const char *trigger_name;
4807 uid_t trigger_owner;
4808 enum lttng_trigger_status trigger_status;
cd9adb8b 4809 struct lttng_error_query_results *results = nullptr;
588c4b0d
JG
4810
4811 switch (lttng_error_query_get_target_type(query)) {
4812 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4813 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4814 break;
63dd3d7b 4815 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
28ab034a 4816 query_target_trigger = lttng_error_query_condition_borrow_target(query);
63dd3d7b 4817 break;
588c4b0d 4818 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
28ab034a 4819 query_target_trigger = lttng_error_query_action_borrow_trigger_target(query);
588c4b0d
JG
4820 break;
4821 default:
4822 abort();
4823 }
4824
a0377dfe 4825 LTTNG_ASSERT(query_target_trigger);
588c4b0d 4826
28ab034a
JG
4827 ret_code = notification_thread_command_get_trigger(
4828 notification_thread, query_target_trigger, &matching_trigger);
588c4b0d
JG
4829 if (ret_code != LTTNG_OK) {
4830 goto end;
4831 }
4832
4833 /* No longer needed. */
cd9adb8b 4834 query_target_trigger = nullptr;
588c4b0d 4835
28ab034a 4836 if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
588c4b0d
JG
4837 /* Get the sessiond-side version of the target action. */
4838 query_target_action =
28ab034a 4839 lttng_error_query_action_borrow_action_target(query, matching_trigger);
588c4b0d
JG
4840 }
4841
4842 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
28ab034a
JG
4843 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4844 trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner);
a0377dfe 4845 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
588c4b0d
JG
4846
4847 results = lttng_error_query_results_create();
4848 if (!results) {
4849 ret_code = LTTNG_ERR_NOMEM;
4850 goto end;
4851 }
4852
4853 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4854 trigger_name,
4855 (int) trigger_owner,
4856 (int) lttng_credentials_get_uid(cmd_creds));
588c4b0d
JG
4857
4858 /*
4859 * Validate the trigger credentials against the command credentials.
4860 * Only the root user can target a trigger with non-matching
4861 * credentials.
4862 */
28ab034a
JG
4863 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger),
4864 cmd_creds)) {
588c4b0d
JG
4865 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4866 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
28ab034a
JG
4867 trigger_name,
4868 (int) trigger_owner,
4869 (int) lttng_credentials_get_uid(cmd_creds));
588c4b0d
JG
4870 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4871 goto end;
4872 }
4873 }
4874
4875 switch (lttng_error_query_get_target_type(query)) {
4876 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
28ab034a 4877 trigger_status = lttng_trigger_add_error_results(matching_trigger, results);
588c4b0d
JG
4878
4879 switch (trigger_status) {
4880 case LTTNG_TRIGGER_STATUS_OK:
4881 break;
4882 default:
4883 ret_code = LTTNG_ERR_UNK;
4884 goto end;
4885 }
4886
4887 break;
63dd3d7b
JG
4888 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4889 {
28ab034a
JG
4890 trigger_status =
4891 lttng_trigger_condition_add_error_results(matching_trigger, results);
63dd3d7b
JG
4892
4893 switch (trigger_status) {
4894 case LTTNG_TRIGGER_STATUS_OK:
4895 break;
4896 default:
4897 ret_code = LTTNG_ERR_UNK;
4898 goto end;
4899 }
4900
4901 break;
4902 }
588c4b0d
JG
4903 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4904 {
4905 const enum lttng_action_status action_status =
28ab034a 4906 lttng_action_add_error_query_results(query_target_action, results);
588c4b0d
JG
4907
4908 switch (action_status) {
4909 case LTTNG_ACTION_STATUS_OK:
4910 break;
4911 default:
4912 ret_code = LTTNG_ERR_UNK;
4913 goto end;
4914 }
4915
4916 break;
4917 }
4918 default:
ef4cf1d2 4919 abort();
588c4b0d
JG
4920 break;
4921 }
4922
4923 *_results = results;
cd9adb8b 4924 results = nullptr;
588c4b0d
JG
4925 ret_code = LTTNG_OK;
4926end:
4927 lttng_trigger_put(matching_trigger);
4928 lttng_error_query_results_destroy(results);
4929 return ret_code;
4930}
4931
6dc3064a
DG
4932/*
4933 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4934 * snapshot output is *not* set with a remote destination.
4935 *
9a654598 4936 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 4937 */
28ab034a
JG
4938static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output,
4939 const struct ltt_session *session)
6dc3064a 4940{
9a654598 4941 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
4942 struct lttng_ht_iter iter;
4943 struct consumer_socket *socket;
1e791a74 4944 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
6fa5fe7c 4945 const char *base_path;
6dc3064a 4946
a0377dfe
FD
4947 LTTNG_ASSERT(output);
4948 LTTNG_ASSERT(session);
6dc3064a
DG
4949
4950 DBG2("Set relayd object from snapshot output");
4951
1e791a74 4952 if (session->current_trace_chunk) {
28ab034a
JG
4953 enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
4954 session->current_trace_chunk, &current_chunk_id.value);
1e791a74 4955
348a81dc 4956 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1e791a74
JG
4957 current_chunk_id.is_set = true;
4958 } else {
4959 ERR("Failed to get current trace chunk id");
4960 status = LTTNG_ERR_UNK;
4961 goto error;
4962 }
4963 }
4964
6dc3064a 4965 /* Ignore if snapshot consumer output is not network. */
348a81dc 4966 if (output->type != CONSUMER_DST_NET) {
6dc3064a
DG
4967 goto error;
4968 }
4969
6fa5fe7c
MD
4970 /*
4971 * The snapshot record URI base path overrides the session
4972 * base path.
4973 */
4974 if (output->dst.net.control.subdir[0] != '\0') {
4975 base_path = output->dst.net.control.subdir;
4976 } else {
4977 base_path = session->base_path;
4978 }
4979
6dc3064a
DG
4980 /*
4981 * For each consumer socket, create and send the relayd object of the
4982 * snapshot output.
4983 */
56047f5a
JG
4984 {
4985 lttng::urcu::read_lock_guard read_lock;
4986
4987 cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
4988 pthread_mutex_lock(socket->lock);
4989 status = send_consumer_relayd_sockets(
4990 session->id,
4991 output,
4992 socket,
4993 session->name,
4994 session->hostname,
4995 base_path,
4996 session->live_timer,
4997 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
4998 session->creation_time,
4999 session->name_contains_creation_time);
5000 pthread_mutex_unlock(socket->lock);
5001 if (status != LTTNG_OK) {
5002 goto error;
5003 }
6dc3064a
DG
5004 }
5005 }
6dc3064a
DG
5006
5007error:
9a654598 5008 return status;
6dc3064a
DG
5009}
5010
5011/*
5012 * Record a kernel snapshot.
5013 *
fac41e72 5014 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 5015 */
28ab034a
JG
5016static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
5017 const struct consumer_output *output,
5018 const struct ltt_session *session,
5019 uint64_t nb_packets_per_stream)
6dc3064a 5020{
9a654598 5021 enum lttng_error_code status;
6dc3064a 5022
a0377dfe
FD
5023 LTTNG_ASSERT(ksess);
5024 LTTNG_ASSERT(output);
5025 LTTNG_ASSERT(session);
6dc3064a 5026
28ab034a 5027 status = kernel_snapshot_record(ksess, output, nb_packets_per_stream);
9a654598 5028 return status;
6dc3064a
DG
5029}
5030
5031/*
5032 * Record a UST snapshot.
5033 *
9a654598 5034 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 5035 */
9a654598 5036static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
28ab034a
JG
5037 const struct consumer_output *output,
5038 const struct ltt_session *session,
5039 uint64_t nb_packets_per_stream)
6dc3064a 5040{
9a654598 5041 enum lttng_error_code status;
6dc3064a 5042
a0377dfe
FD
5043 LTTNG_ASSERT(usess);
5044 LTTNG_ASSERT(output);
5045 LTTNG_ASSERT(session);
6dc3064a 5046
28ab034a 5047 status = ust_app_snapshot_record(usess, output, nb_packets_per_stream);
9a654598 5048 return status;
6dc3064a
DG
5049}
5050
28ab034a
JG
5051static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session,
5052 uint64_t cur_nr_packets)
68808f4e 5053{
d07ceecd 5054 uint64_t tot_size = 0;
68808f4e
DG
5055
5056 if (session->kernel_session) {
5057 struct ltt_kernel_channel *chan;
28ab034a 5058 const struct ltt_kernel_session *ksess = session->kernel_session;
68808f4e 5059
28ab034a 5060 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
d07ceecd
MD
5061 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5062 /*
5063 * Don't take channel into account if we
5064 * already grab all its packets.
5065 */
5066 continue;
68808f4e 5067 }
28ab034a 5068 tot_size += chan->channel->attr.subbuf_size * chan->stream_count;
68808f4e
DG
5069 }
5070 }
5071
5072 if (session->ust_session) {
fb9a95c4 5073 const struct ltt_ust_session *usess = session->ust_session;
68808f4e 5074
28ab034a 5075 tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets);
68808f4e
DG
5076 }
5077
d07ceecd 5078 return tot_size;
68808f4e
DG
5079}
5080
5c786ded 5081/*
d07ceecd
MD
5082 * Calculate the number of packets we can grab from each stream that
5083 * fits within the overall snapshot max size.
5084 *
5085 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5086 * the number of packets per stream.
5087 *
5088 * TODO: this approach is not perfect: we consider the worse case
5089 * (packet filling the sub-buffers) as an upper bound, but we could do
5090 * better if we do this calculation while we actually grab the packet
5091 * content: we would know how much padding we don't actually store into
5092 * the file.
5093 *
5094 * This algorithm is currently bounded by the number of packets per
5095 * stream.
5096 *
5097 * Since we call this algorithm before actually grabbing the data, it's
5098 * an approximation: for instance, applications could appear/disappear
5099 * in between this call and actually grabbing data.
5c786ded 5100 */
28ab034a
JG
5101static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5102 uint64_t max_size)
5c786ded 5103{
d07ceecd
MD
5104 int64_t size_left;
5105 uint64_t cur_nb_packets = 0;
5c786ded 5106
d07ceecd 5107 if (!max_size) {
28ab034a 5108 return 0; /* Infinite */
5c786ded
JD
5109 }
5110
d07ceecd
MD
5111 size_left = max_size;
5112 for (;;) {
5113 uint64_t one_more_packet_tot_size;
5c786ded 5114
28ab034a
JG
5115 one_more_packet_tot_size =
5116 get_session_size_one_more_packet_per_stream(session, cur_nb_packets);
d07ceecd
MD
5117 if (!one_more_packet_tot_size) {
5118 /* We are already grabbing all packets. */
5119 break;
5120 }
5121 size_left -= one_more_packet_tot_size;
5122 if (size_left < 0) {
5123 break;
5124 }
5125 cur_nb_packets++;
5c786ded 5126 }
aecf2da5 5127 if (!cur_nb_packets && size_left != max_size) {
d07ceecd
MD
5128 /* Not enough room to grab one packet of each stream, error. */
5129 return -1;
5130 }
5131 return cur_nb_packets;
5c786ded
JD
5132}
5133
28ab034a
JG
5134static enum lttng_error_code snapshot_record(struct ltt_session *session,
5135 const struct snapshot_output *snapshot_output)
fb9a95c4
JG
5136{
5137 int64_t nb_packets_per_stream;
d2956687 5138 char snapshot_chunk_name[LTTNG_NAME_MAX];
348a81dc
JG
5139 int ret;
5140 enum lttng_error_code ret_code = LTTNG_OK;
d2956687 5141 struct lttng_trace_chunk *snapshot_trace_chunk;
cd9adb8b
JG
5142 struct consumer_output *original_ust_consumer_output = nullptr;
5143 struct consumer_output *original_kernel_consumer_output = nullptr;
5144 struct consumer_output *snapshot_ust_consumer_output = nullptr;
5145 struct consumer_output *snapshot_kernel_consumer_output = nullptr;
d2956687 5146
28ab034a
JG
5147 ret = snprintf(snapshot_chunk_name,
5148 sizeof(snapshot_chunk_name),
5149 "%s-%s-%" PRIu64,
5150 snapshot_output->name,
5151 snapshot_output->datetime,
5152 snapshot_output->nb_snapshot);
348a81dc 5153 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
d2956687 5154 ERR("Failed to format snapshot name");
348a81dc
JG
5155 ret_code = LTTNG_ERR_INVALID;
5156 goto error;
d2956687
JG
5157 }
5158 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
28ab034a
JG
5159 snapshot_output->name,
5160 session->name,
5161 snapshot_chunk_name);
348a81dc
JG
5162 if (!session->kernel_session && !session->ust_session) {
5163 ERR("Failed to record snapshot as no channels exist");
5164 ret_code = LTTNG_ERR_NO_CHANNEL;
5165 goto error;
5166 }
5167
5168 if (session->kernel_session) {
28ab034a
JG
5169 original_kernel_consumer_output = session->kernel_session->consumer;
5170 snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer);
5171 strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name);
bd666153
JR
5172
5173 /* Copy the original domain subdir. */
5174 strcpy(snapshot_kernel_consumer_output->domain_subdir,
28ab034a 5175 original_kernel_consumer_output->domain_subdir);
bd666153 5176
348a81dc 5177 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
28ab034a 5178 original_kernel_consumer_output);
348a81dc
JG
5179 if (ret < 0) {
5180 ERR("Failed to copy consumer sockets from snapshot output configuration");
5181 ret_code = LTTNG_ERR_NOMEM;
5182 goto error;
5183 }
28ab034a 5184 ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session);
348a81dc
JG
5185 if (ret_code != LTTNG_OK) {
5186 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5187 goto error;
5188 }
28ab034a 5189 session->kernel_session->consumer = snapshot_kernel_consumer_output;
348a81dc
JG
5190 }
5191 if (session->ust_session) {
5192 original_ust_consumer_output = session->ust_session->consumer;
28ab034a
JG
5193 snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer);
5194 strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name);
bd666153
JR
5195
5196 /* Copy the original domain subdir. */
5197 strcpy(snapshot_ust_consumer_output->domain_subdir,
28ab034a 5198 original_ust_consumer_output->domain_subdir);
bd666153 5199
348a81dc 5200 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
28ab034a 5201 original_ust_consumer_output);
348a81dc
JG
5202 if (ret < 0) {
5203 ERR("Failed to copy consumer sockets from snapshot output configuration");
5204 ret_code = LTTNG_ERR_NOMEM;
5205 goto error;
5206 }
28ab034a 5207 ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session);
348a81dc
JG
5208 if (ret_code != LTTNG_OK) {
5209 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5210 goto error;
5211 }
28ab034a 5212 session->ust_session->consumer = snapshot_ust_consumer_output;
348a81dc
JG
5213 }
5214
28ab034a
JG
5215 snapshot_trace_chunk = session_create_new_trace_chunk(
5216 session,
5217 snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output,
5218 consumer_output_get_base_path(snapshot_output->consumer),
5219 snapshot_chunk_name);
d2956687 5220 if (!snapshot_trace_chunk) {
348a81dc 5221 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
28ab034a 5222 session->name);
348a81dc
JG
5223 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5224 goto error;
d2956687 5225 }
a0377dfe 5226 LTTNG_ASSERT(!session->current_trace_chunk);
cd9adb8b 5227 ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr);
d2956687 5228 lttng_trace_chunk_put(snapshot_trace_chunk);
cd9adb8b 5229 snapshot_trace_chunk = nullptr;
d2956687 5230 if (ret) {
348a81dc 5231 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
28ab034a 5232 session->name);
348a81dc
JG
5233 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5234 goto error;
d2956687 5235 }
fb9a95c4 5236
28ab034a
JG
5237 nb_packets_per_stream =
5238 get_session_nb_packets_per_stream(session, snapshot_output->max_size);
fb9a95c4 5239 if (nb_packets_per_stream < 0) {
348a81dc 5240 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5151d412 5241 goto error_close_trace_chunk;
fb9a95c4
JG
5242 }
5243
5244 if (session->kernel_session) {
348a81dc 5245 ret_code = record_kernel_snapshot(session->kernel_session,
28ab034a
JG
5246 snapshot_kernel_consumer_output,
5247 session,
5248 nb_packets_per_stream);
348a81dc 5249 if (ret_code != LTTNG_OK) {
5151d412 5250 goto error_close_trace_chunk;
fb9a95c4
JG
5251 }
5252 }
5253
5254 if (session->ust_session) {
348a81dc 5255 ret_code = record_ust_snapshot(session->ust_session,
28ab034a
JG
5256 snapshot_ust_consumer_output,
5257 session,
5258 nb_packets_per_stream);
348a81dc 5259 if (ret_code != LTTNG_OK) {
5151d412 5260 goto error_close_trace_chunk;
fb9a95c4
JG
5261 }
5262 }
d2956687 5263
5151d412 5264error_close_trace_chunk:
cd9adb8b 5265 if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) {
28ab034a 5266 ERR("Failed to release the current trace chunk of session \"%s\"", session->name);
dbfee52c
MD
5267 ret_code = LTTNG_ERR_UNK;
5268 }
5269
28ab034a
JG
5270 if (session_close_trace_chunk(session,
5271 snapshot_trace_chunk,
5272 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION,
cd9adb8b 5273 nullptr)) {
d2956687
JG
5274 /*
5275 * Don't goto end; make sure the chunk is closed for the session
5276 * to allow future snapshots.
5277 */
28ab034a 5278 ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name);
348a81dc 5279 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
d2956687 5280 }
a49a9216
JG
5281
5282 lttng_trace_chunk_put(snapshot_trace_chunk);
cd9adb8b 5283 snapshot_trace_chunk = nullptr;
348a81dc
JG
5284error:
5285 if (original_ust_consumer_output) {
5286 session->ust_session->consumer = original_ust_consumer_output;
5287 }
5288 if (original_kernel_consumer_output) {
28ab034a 5289 session->kernel_session->consumer = original_kernel_consumer_output;
348a81dc
JG
5290 }
5291 consumer_output_put(snapshot_ust_consumer_output);
5292 consumer_output_put(snapshot_kernel_consumer_output);
5293 return ret_code;
fb9a95c4
JG
5294}
5295
6dc3064a
DG
5296/*
5297 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5298 *
5299 * The wait parameter is ignored so this call always wait for the snapshot to
5300 * complete before returning.
5301 *
5302 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5303 */
5304int cmd_snapshot_record(struct ltt_session *session,
28ab034a
JG
5305 const struct lttng_snapshot_output *output,
5306 int wait __attribute__((unused)))
6dc3064a 5307{
9a654598
JG
5308 enum lttng_error_code cmd_ret = LTTNG_OK;
5309 int ret;
00e1dfc4 5310 unsigned int snapshot_success = 0;
10ba83fe 5311 char datetime[16];
cd9adb8b 5312 struct snapshot_output *tmp_output = nullptr;
6dc3064a 5313
a0377dfe
FD
5314 LTTNG_ASSERT(session);
5315 LTTNG_ASSERT(output);
6dc3064a
DG
5316
5317 DBG("Cmd snapshot record for session %s", session->name);
5318
10ba83fe 5319 /* Get the datetime for the snapshot output directory. */
28ab034a 5320 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime));
10ba83fe 5321 if (!ret) {
9a654598 5322 cmd_ret = LTTNG_ERR_INVALID;
10ba83fe
JR
5323 goto error;
5324 }
5325
6dc3064a 5326 /*
d3f14b8a
MD
5327 * Permission denied to create an output if the session is not
5328 * set in no output mode.
6dc3064a
DG
5329 */
5330 if (session->output_traces) {
9a654598 5331 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
5332 goto error;
5333 }
5334
5335 /* The session needs to be started at least once. */
8382cf6f 5336 if (!session->has_been_started) {
9a654598 5337 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
6dc3064a
DG
5338 goto error;
5339 }
5340
5341 /* Use temporary output for the session. */
ba45d9f0 5342 if (*output->ctrl_url != '\0') {
2abe7969
JG
5343 tmp_output = snapshot_output_alloc();
5344 if (!tmp_output) {
5345 cmd_ret = LTTNG_ERR_NOMEM;
5346 goto error;
5347 }
5348
28ab034a
JG
5349 ret = snapshot_output_init(session,
5350 output->max_size,
5351 output->name,
5352 output->ctrl_url,
5353 output->data_url,
5354 session->consumer,
5355 tmp_output,
cd9adb8b 5356 nullptr);
6dc3064a
DG
5357 if (ret < 0) {
5358 if (ret == -ENOMEM) {
9a654598 5359 cmd_ret = LTTNG_ERR_NOMEM;
6dc3064a 5360 } else {
9a654598 5361 cmd_ret = LTTNG_ERR_INVALID;
6dc3064a
DG
5362 }
5363 goto error;
5364 }
1bfe7328 5365 /* Use the global session count for the temporary snapshot. */
2abe7969 5366 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
10ba83fe
JR
5367
5368 /* Use the global datetime */
2abe7969 5369 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
f46376a1 5370 cmd_ret = snapshot_record(session, tmp_output);
fb9a95c4 5371 if (cmd_ret != LTTNG_OK) {
804c90a8
JR
5372 goto error;
5373 }
804c90a8
JR
5374 snapshot_success = 1;
5375 } else {
5376 struct snapshot_output *sout;
5377 struct lttng_ht_iter iter;
68808f4e 5378
56047f5a
JG
5379 lttng::urcu::read_lock_guard read_lock;
5380
28ab034a
JG
5381 cds_lfht_for_each_entry (
5382 session->snapshot.output_ht->ht, &iter.iter, sout, node.node) {
2abe7969
JG
5383 struct snapshot_output output_copy;
5384
804c90a8 5385 /*
2abe7969
JG
5386 * Make a local copy of the output and override output
5387 * parameters with those provided as part of the
5388 * command.
804c90a8 5389 */
2abe7969 5390 memcpy(&output_copy, sout, sizeof(output_copy));
1bfe7328 5391
804c90a8 5392 if (output->max_size != (uint64_t) -1ULL) {
2abe7969 5393 output_copy.max_size = output->max_size;
6dc3064a 5394 }
d07ceecd 5395
2abe7969 5396 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
28ab034a 5397 memcpy(output_copy.datetime, datetime, sizeof(datetime));
6dc3064a 5398
804c90a8
JR
5399 /* Use temporary name. */
5400 if (*output->name != '\0') {
2abe7969 5401 if (lttng_strncpy(output_copy.name,
28ab034a
JG
5402 output->name,
5403 sizeof(output_copy.name))) {
9a654598 5404 cmd_ret = LTTNG_ERR_INVALID;
cf3e357d
MD
5405 goto error;
5406 }
804c90a8 5407 }
e1986656 5408
f46376a1 5409 cmd_ret = snapshot_record(session, &output_copy);
fb9a95c4 5410 if (cmd_ret != LTTNG_OK) {
fb9a95c4 5411 goto error;
6dc3064a 5412 }
56047f5a 5413
804c90a8 5414 snapshot_success = 1;
6dc3064a
DG
5415 }
5416 }
5417
1bfe7328
DG
5418 if (snapshot_success) {
5419 session->snapshot.nb_snapshot++;
b67578cb 5420 } else {
9a654598 5421 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
5422 }
5423
6dc3064a 5424error:
2abe7969
JG
5425 if (tmp_output) {
5426 snapshot_output_destroy(tmp_output);
5427 }
56047f5a 5428
9a654598 5429 return cmd_ret;
6dc3064a
DG
5430}
5431
d7ba1388
MD
5432/*
5433 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5434 */
28ab034a 5435int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
d7ba1388
MD
5436{
5437 /* Safety net */
a0377dfe 5438 LTTNG_ASSERT(session);
d7ba1388
MD
5439
5440 /*
5441 * Can only set shm path before session is started.
5442 */
5443 if (session->has_been_started) {
5444 return LTTNG_ERR_SESSION_STARTED;
5445 }
5446
28ab034a 5447 strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
d7ba1388
MD
5448 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5449
7e397c55 5450 return LTTNG_OK;
d7ba1388
MD
5451}
5452
5c408ad8
JD
5453/*
5454 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5455 *
5456 * Ask the consumer to rotate the session output directory.
5457 * The session lock must be held.
5458 *
d5a1b7aa 5459 * Returns LTTNG_OK on success or else a negative LTTng error code.
5c408ad8
JD
5460 */
5461int cmd_rotate_session(struct ltt_session *session,
28ab034a
JG
5462 struct lttng_rotate_session_return *rotate_return,
5463 bool quiet_rotation,
5464 enum lttng_trace_chunk_command_type command)
5c408ad8
JD
5465{
5466 int ret;
d2956687 5467 uint64_t ongoing_rotation_chunk_id;
d5a1b7aa 5468 enum lttng_error_code cmd_ret = LTTNG_OK;
cd9adb8b
JG
5469 struct lttng_trace_chunk *chunk_being_archived = nullptr;
5470 struct lttng_trace_chunk *new_trace_chunk = nullptr;
d2956687 5471 enum lttng_trace_chunk_status chunk_status;
3156892b
JG
5472 bool failed_to_rotate = false;
5473 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5c408ad8 5474
a0377dfe 5475 LTTNG_ASSERT(session);
5c408ad8
JD
5476
5477 if (!session->has_been_started) {
d5a1b7aa 5478 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
d68c9a04 5479 goto end;
5c408ad8
JD
5480 }
5481
d48d65e1
MD
5482 /*
5483 * Explicit rotation is not supported for live sessions.
5484 * However, live sessions can perform a quiet rotation on
5485 * destroy.
5486 * Rotation is not supported for snapshot traces (no output).
5487 */
28ab034a 5488 if ((!quiet_rotation && session->live_timer) || !session->output_traces) {
d5a1b7aa 5489 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
d68c9a04 5490 goto end;
5c408ad8
JD
5491 }
5492
d2956687 5493 /* Unsupported feature in lttng-relayd before 2.11. */
070b6a86 5494 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
28ab034a
JG
5495 (session->consumer->relay_major_version == 2 &&
5496 session->consumer->relay_minor_version < 11)) {
d5a1b7aa 5497 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
d68c9a04 5498 goto end;
5c408ad8
JD
5499 }
5500
a40a503f
MD
5501 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5502 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5503 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5504 goto end;
5505 }
5506
92816cc3 5507 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
92816cc3 5508 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
28ab034a 5509 session->name);
d5a1b7aa 5510 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
d68c9a04 5511 goto end;
5c408ad8
JD
5512 }
5513
5514 /*
5515 * After a stop, we only allow one rotation to occur, the other ones are
5516 * useless until a new start.
5517 */
5518 if (session->rotated_after_last_stop) {
5519 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
28ab034a 5520 session->name);
d5a1b7aa 5521 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
d68c9a04 5522 goto end;
5c408ad8 5523 }
b02f5986
MD
5524
5525 /*
5526 * After a stop followed by a clear, disallow following rotations a they would
5527 * generate empty chunks.
5528 */
5529 if (session->cleared_after_last_stop) {
5530 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
28ab034a 5531 session->name);
b02f5986
MD
5532 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5533 goto end;
5534 }
5535
d2956687 5536 if (session->active) {
cd9adb8b
JG
5537 new_trace_chunk =
5538 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
d2956687
JG
5539 if (!new_trace_chunk) {
5540 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5541 goto error;
5c408ad8 5542 }
0e270a1e 5543 }
2961f09e 5544
3156892b
JG
5545 /*
5546 * The current trace chunk becomes the chunk being archived.
5547 *
5548 * After this point, "chunk_being_archived" must absolutely
5549 * be closed on the consumer(s), otherwise it will never be
5550 * cleaned-up, which will result in a leak.
5551 */
28ab034a 5552 ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived);
d2956687
JG
5553 if (ret) {
5554 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
b178f53e
JG
5555 goto error;
5556 }
5557
5c408ad8 5558 if (session->kernel_session) {
d5a1b7aa
JG
5559 cmd_ret = kernel_rotate_session(session);
5560 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5561 failed_to_rotate = true;
5562 rotation_fail_code = cmd_ret;
5c408ad8
JD
5563 }
5564 }
5565 if (session->ust_session) {
d5a1b7aa
JG
5566 cmd_ret = ust_app_rotate_session(session);
5567 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5568 failed_to_rotate = true;
5569 rotation_fail_code = cmd_ret;
5c408ad8 5570 }
92816cc3 5571 }
17dd1232 5572
3b61d9ee
JG
5573 if (!session->active) {
5574 session->rotated_after_last_stop = true;
5575 }
5576
5577 if (!chunk_being_archived) {
5578 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
28ab034a 5579 session->name);
3b61d9ee
JG
5580 if (failed_to_rotate) {
5581 cmd_ret = rotation_fail_code;
5582 goto error;
5583 }
5584 cmd_ret = LTTNG_OK;
5585 goto end;
5586 }
5587
5588 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
28ab034a 5589 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id);
a0377dfe 5590 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
3b61d9ee 5591
28ab034a
JG
5592 ret = session_close_trace_chunk(
5593 session, chunk_being_archived, command, session->last_chunk_path);
d2956687
JG
5594 if (ret) {
5595 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5596 goto error;
5597 }
5598
3156892b
JG
5599 if (failed_to_rotate) {
5600 cmd_ret = rotation_fail_code;
5601 goto error;
5602 }
5603
7fdbed1c 5604 session->quiet_rotation = quiet_rotation;
28ab034a 5605 ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER);
92816cc3 5606 if (ret) {
d5a1b7aa 5607 cmd_ret = LTTNG_ERR_UNK;
2961f09e 5608 goto error;
5c408ad8
JD
5609 }
5610
5c408ad8 5611 if (rotate_return) {
d2956687 5612 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5c408ad8
JD
5613 }
5614
d2956687 5615 session->chunk_being_archived = chunk_being_archived;
cd9adb8b 5616 chunk_being_archived = nullptr;
7fdbed1c
JG
5617 if (!quiet_rotation) {
5618 ret = notification_thread_command_session_rotation_ongoing(
28ab034a 5619 the_notification_thread_handle, session->id, ongoing_rotation_chunk_id);
7fdbed1c
JG
5620 if (ret != LTTNG_OK) {
5621 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
28ab034a 5622 session->name);
7966af57 5623 cmd_ret = (lttng_error_code) ret;
7fdbed1c 5624 }
2961f09e
JG
5625 }
5626
92816cc3 5627 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
28ab034a
JG
5628 session->name,
5629 ongoing_rotation_chunk_id);
5c408ad8 5630end:
d2956687
JG
5631 lttng_trace_chunk_put(new_trace_chunk);
5632 lttng_trace_chunk_put(chunk_being_archived);
d5a1b7aa 5633 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5c408ad8 5634 return ret;
2961f09e 5635error:
0038180d 5636 if (session_reset_rotation_state(*session, LTTNG_ROTATION_STATE_ERROR)) {
28ab034a 5637 ERR("Failed to reset rotation state of session \"%s\"", session->name);
2961f09e
JG
5638 }
5639 goto end;
5c408ad8
JD
5640}
5641
5642/*
d68c9a04 5643 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5c408ad8
JD
5644 *
5645 * Check if the session has finished its rotation.
5646 *
d2956687 5647 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5c408ad8 5648 */
d68c9a04 5649int cmd_rotate_get_info(struct ltt_session *session,
28ab034a
JG
5650 struct lttng_rotation_get_info_return *info_return,
5651 uint64_t rotation_id)
5c408ad8 5652{
d2956687
JG
5653 enum lttng_error_code cmd_ret = LTTNG_OK;
5654 enum lttng_rotation_state rotation_state;
5c408ad8 5655
28ab034a
JG
5656 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64,
5657 session->name,
5658 session->most_recent_chunk_id.value);
5c408ad8 5659
d2956687
JG
5660 if (session->chunk_being_archived) {
5661 enum lttng_trace_chunk_status chunk_status;
5662 uint64_t chunk_id;
5663
28ab034a 5664 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
a0377dfe 5665 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
d2956687 5666
28ab034a
JG
5667 rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING :
5668 LTTNG_ROTATION_STATE_EXPIRED;
d2956687
JG
5669 } else {
5670 if (session->last_archived_chunk_id.is_set &&
28ab034a 5671 rotation_id != session->last_archived_chunk_id.value) {
d2956687
JG
5672 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5673 } else {
5674 rotation_state = session->rotation_state;
5675 }
5c408ad8
JD
5676 }
5677
d2956687
JG
5678 switch (rotation_state) {
5679 case LTTNG_ROTATION_STATE_NO_ROTATION:
83ed9e90 5680 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
28ab034a 5681 session->name);
d2956687
JG
5682 goto end;
5683 case LTTNG_ROTATION_STATE_EXPIRED:
28ab034a
JG
5684 DBG("Reporting that the rotation state of rotation id %" PRIu64
5685 " of session \"%s\" has expired",
5686 rotation_id,
5687 session->name);
d2956687 5688 break;
d68c9a04 5689 case LTTNG_ROTATION_STATE_ONGOING:
d2956687 5690 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
28ab034a
JG
5691 rotation_id,
5692 session->name);
d68c9a04
JD
5693 break;
5694 case LTTNG_ROTATION_STATE_COMPLETED:
dd73d57b 5695 {
d2956687
JG
5696 int fmt_ret;
5697 char *chunk_path;
dd73d57b
JG
5698 char *current_tracing_path_reply;
5699 size_t current_tracing_path_reply_len;
5700
d2956687 5701 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
28ab034a
JG
5702 rotation_id,
5703 session->name);
d2956687 5704
dd73d57b
JG
5705 switch (session_get_consumer_destination_type(session)) {
5706 case CONSUMER_DST_LOCAL:
28ab034a 5707 current_tracing_path_reply = info_return->location.local.absolute_path;
dd73d57b 5708 current_tracing_path_reply_len =
28ab034a 5709 sizeof(info_return->location.local.absolute_path);
dd73d57b 5710 info_return->location_type =
28ab034a 5711 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
ecd1a12f 5712 fmt_ret = asprintf(&chunk_path,
28ab034a
JG
5713 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5714 session_get_base_path(session),
5715 session->last_archived_chunk_name);
ecd1a12f
MD
5716 if (fmt_ret == -1) {
5717 PERROR("Failed to format the path of the last archived trace chunk");
5718 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5719 cmd_ret = LTTNG_ERR_UNK;
5720 goto end;
5721 }
dd73d57b
JG
5722 break;
5723 case CONSUMER_DST_NET:
09cfbe47
JG
5724 {
5725 uint16_t ctrl_port, data_port;
5726
28ab034a 5727 current_tracing_path_reply = info_return->location.relay.relative_path;
dd73d57b 5728 current_tracing_path_reply_len =
28ab034a 5729 sizeof(info_return->location.relay.relative_path);
dd73d57b
JG
5730 /* Currently the only supported relay protocol. */
5731 info_return->location.relay.protocol =
28ab034a 5732 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
dd73d57b 5733
d2956687 5734 fmt_ret = lttng_strncpy(info_return->location.relay.host,
28ab034a
JG
5735 session_get_net_consumer_hostname(session),
5736 sizeof(info_return->location.relay.host));
d2956687
JG
5737 if (fmt_ret) {
5738 ERR("Failed to copy host name to rotate_get_info reply");
dd73d57b 5739 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5740 cmd_ret = LTTNG_ERR_SET_URL;
dd73d57b
JG
5741 goto end;
5742 }
5743
09cfbe47
JG
5744 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5745 info_return->location.relay.ports.control = ctrl_port;
5746 info_return->location.relay.ports.data = data_port;
dd73d57b 5747 info_return->location_type =
28ab034a 5748 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
ecd1a12f
MD
5749 chunk_path = strdup(session->last_chunk_path);
5750 if (!chunk_path) {
5751 ERR("Failed to allocate the path of the last archived trace chunk");
5752 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5753 cmd_ret = LTTNG_ERR_UNK;
5754 goto end;
5755 }
dd73d57b 5756 break;
09cfbe47 5757 }
dd73d57b
JG
5758 default:
5759 abort();
5760 }
d2956687 5761
28ab034a
JG
5762 fmt_ret = lttng_strncpy(
5763 current_tracing_path_reply, chunk_path, current_tracing_path_reply_len);
d2956687
JG
5764 free(chunk_path);
5765 if (fmt_ret) {
5766 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
d68c9a04 5767 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5768 cmd_ret = LTTNG_ERR_UNK;
5c408ad8
JD
5769 goto end;
5770 }
dd73d57b 5771
d68c9a04 5772 break;
dd73d57b 5773 }
d68c9a04 5774 case LTTNG_ROTATION_STATE_ERROR:
28ab034a
JG
5775 DBG("Reporting that an error occurred during rotation %" PRIu64
5776 " of session \"%s\"",
5777 rotation_id,
5778 session->name);
d68c9a04
JD
5779 break;
5780 default:
5781 abort();
5c408ad8
JD
5782 }
5783
d2956687 5784 cmd_ret = LTTNG_OK;
5c408ad8 5785end:
d2956687
JG
5786 info_return->status = (int32_t) rotation_state;
5787 return cmd_ret;
5c408ad8
JD
5788}
5789
259c2674
JD
5790/*
5791 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5792 *
5793 * Configure the automatic rotation parameters.
66ea93b1
JG
5794 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5795 * 'activate' to false means deactivate the rotation schedule and validate that
5796 * 'new_value' has the same value as the currently active value.
259c2674 5797 *
1136f41b 5798 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
259c2674
JD
5799 */
5800int cmd_rotation_set_schedule(struct ltt_session *session,
28ab034a
JG
5801 bool activate,
5802 enum lttng_rotation_schedule_type schedule_type,
0038180d 5803 uint64_t new_value)
259c2674
JD
5804{
5805 int ret;
66ea93b1 5806 uint64_t *parameter_value;
259c2674 5807
a0377dfe 5808 LTTNG_ASSERT(session);
259c2674
JD
5809
5810 DBG("Cmd rotate set schedule session %s", session->name);
5811
92fe5ca1 5812 if (session->live_timer || !session->output_traces) {
66ea93b1 5813 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
259c2674
JD
5814 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5815 goto end;
5816 }
5817
66ea93b1
JG
5818 switch (schedule_type) {
5819 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5820 parameter_value = &session->rotate_size;
5821 break;
5822 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5823 parameter_value = &session->rotate_timer_period;
5824 if (new_value >= UINT_MAX) {
28ab034a
JG
5825 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5826 " > %u (UINT_MAX)",
5827 new_value,
5828 UINT_MAX);
66ea93b1
JG
5829 ret = LTTNG_ERR_INVALID;
5830 goto end;
5831 }
5832 break;
5833 default:
5834 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5835 ret = LTTNG_ERR_INVALID;
259c2674 5836 goto end;
66ea93b1
JG
5837 }
5838
5839 /* Improper use of the API. */
5840 if (new_value == -1ULL) {
5841 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5842 ret = LTTNG_ERR_INVALID;
259c2674
JD
5843 goto end;
5844 }
5845
66ea93b1
JG
5846 /*
5847 * As indicated in struct ltt_session's comments, a value of == 0 means
5848 * this schedule rotation type is not in use.
5849 *
5850 * Reject the command if we were asked to activate a schedule that was
5851 * already active.
5852 */
5853 if (activate && *parameter_value != 0) {
5854 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5855 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
90936dcf 5856 goto end;
66ea93b1
JG
5857 }
5858
5859 /*
5860 * Reject the command if we were asked to deactivate a schedule that was
5861 * not active.
5862 */
5863 if (!activate && *parameter_value == 0) {
5864 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5865 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
90936dcf
JD
5866 goto end;
5867 }
5868
66ea93b1
JG
5869 /*
5870 * Reject the command if we were asked to deactivate a schedule that
5871 * doesn't exist.
5872 */
5873 if (!activate && *parameter_value != new_value) {
5874 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5875 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5876 goto end;
5877 }
259c2674 5878
66ea93b1
JG
5879 *parameter_value = activate ? new_value : 0;
5880
5881 switch (schedule_type) {
5882 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5883 if (activate && session->active) {
5884 /*
5885 * Only start the timer if the session is active,
5886 * otherwise it will be started when the session starts.
5887 */
28ab034a 5888 ret = timer_session_rotation_schedule_timer_start(session, new_value);
259c2674 5889 if (ret) {
66ea93b1 5890 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
259c2674
JD
5891 ret = LTTNG_ERR_UNK;
5892 goto end;
5893 }
66ea93b1 5894 } else {
28ab034a 5895 ret = timer_session_rotation_schedule_timer_stop(session);
66ea93b1
JG
5896 if (ret) {
5897 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5898 ret = LTTNG_ERR_UNK;
f3ce6946 5899 goto end;
66ea93b1 5900 }
259c2674 5901 }
66ea93b1
JG
5902 break;
5903 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5904 if (activate) {
0038180d
JG
5905 try {
5906 the_rotation_thread_handle->subscribe_session_consumed_size_rotation(
5907 *session, new_value);
5b9eda8a 5908 } catch (const std::exception& e) {
0038180d
JG
5909 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5910 e.what());
90936dcf
JD
5911 ret = LTTNG_ERR_UNK;
5912 goto end;
5913 }
90936dcf 5914 } else {
0038180d
JG
5915 try {
5916 the_rotation_thread_handle
5917 ->unsubscribe_session_consumed_size_rotation(*session);
5b9eda8a 5918 } catch (const std::exception& e) {
0038180d
JG
5919 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5920 e.what());
90936dcf
JD
5921 ret = LTTNG_ERR_UNK;
5922 goto end;
5923 }
90936dcf 5924 }
66ea93b1
JG
5925 break;
5926 default:
5927 /* Would have been caught before. */
5928 abort();
90936dcf
JD
5929 }
5930
259c2674
JD
5931 ret = LTTNG_OK;
5932
5933 goto end;
5934
5935end:
5936 return ret;
5937}
5938
a503e1ef
JG
5939/* Wait for a given path to be removed before continuing. */
5940static enum lttng_error_code wait_on_path(void *path_data)
5941{
7966af57 5942 const char *shm_path = (const char *) path_data;
a503e1ef
JG
5943
5944 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
28ab034a 5945 shm_path);
a503e1ef
JG
5946 while (true) {
5947 int ret;
5948 struct stat st;
5949
5950 ret = stat(shm_path, &st);
5951 if (ret) {
5952 if (errno != ENOENT) {
5953 PERROR("stat() returned an error while checking for the existence of the shm path");
5954 } else {
5955 DBG("shm path no longer exists, completing the destruction of session");
5956 }
5957 break;
5958 } else {
5959 if (!S_ISDIR(st.st_mode)) {
5960 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
28ab034a 5961 shm_path);
a503e1ef
JG
5962 break;
5963 }
5964 }
5965 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5966 }
5967 return LTTNG_OK;
5968}
5969
5970/*
5971 * Returns a pointer to a handler to run on completion of a command.
5972 * Returns NULL if no handler has to be run for the last command executed.
5973 */
cd9adb8b 5974const struct cmd_completion_handler *cmd_pop_completion_handler()
a503e1ef
JG
5975{
5976 struct cmd_completion_handler *handler = current_completion_handler;
5977
cd9adb8b 5978 current_completion_handler = nullptr;
a503e1ef
JG
5979 return handler;
5980}
5981
2f77fc4b
DG
5982/*
5983 * Init command subsystem.
5984 */
cd9adb8b 5985void cmd_init()
2f77fc4b
DG
5986{
5987 /*
d88aee68
DG
5988 * Set network sequence index to 1 for streams to match a relayd
5989 * socket on the consumer side.
2f77fc4b 5990 */
d88aee68
DG
5991 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5992 relayd_net_seq_idx = 1;
5993 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
5994
5995 DBG("Command subsystem initialized");
5996}
This page took 0.560734 seconds and 4 git commands to generate.