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