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