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