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