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