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