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