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