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