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