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