Fix: sessiond: don't negate error code on list error
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
CommitLineData
2f77fc4b 1/*
ab5be9fa
MJ
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
2f77fc4b 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
2f77fc4b 6 *
2f77fc4b
DG
7 */
8
159b042f 9#include "bin/lttng-sessiond/tracker.h"
c0a66c84 10#include "common/sessiond-comm/payload.h"
159b042f
JG
11#include "lttng/lttng-error.h"
12#include "lttng/tracker.h"
6c1c0768 13#define _LGPL_SOURCE
2f77fc4b 14#include <assert.h>
6dc3064a 15#include <inttypes.h>
2f77fc4b
DG
16#include <urcu/list.h>
17#include <urcu/uatomic.h>
a503e1ef 18#include <sys/stat.h>
d2956687 19#include <stdio.h>
2f77fc4b
DG
20
21#include <common/defaults.h>
22#include <common/common.h>
23#include <common/sessiond-comm/sessiond-comm.h>
24#include <common/relayd/relayd.h>
10a50311 25#include <common/utils.h>
f5436bfc 26#include <common/compat/string.h>
93ec662e 27#include <common/kernel-ctl/kernel-ctl.h>
b0880ae5
JG
28#include <common/dynamic-buffer.h>
29#include <common/buffer-view.h>
c0a66c84
JG
30#include <common/sessiond-comm/payload.h>
31#include <common/sessiond-comm/payload-view.h>
82b69413 32#include <common/trace-chunk.h>
3e3665b8 33#include <lttng/location-internal.h>
b0880ae5
JG
34#include <lttng/trigger/trigger-internal.h>
35#include <lttng/condition/condition.h>
36#include <lttng/action/action.h>
6e21424e 37#include <lttng/channel.h>
b0880ae5 38#include <lttng/channel-internal.h>
5c408ad8 39#include <lttng/rotate-internal.h>
17dd1232 40#include <lttng/location-internal.h>
b178f53e 41#include <lttng/session-internal.h>
3c02e545 42#include <lttng/userspace-probe-internal.h>
b178f53e 43#include <lttng/session-descriptor-internal.h>
9f449915 44#include <common/string-utils/string-utils.h>
2f77fc4b
DG
45
46#include "channel.h"
47#include "consumer.h"
48#include "event.h"
8782cc74 49#include "health-sessiond.h"
2f77fc4b
DG
50#include "kernel.h"
51#include "kernel-consumer.h"
52#include "lttng-sessiond.h"
53#include "utils.h"
0dbc2034 54#include "lttng-syscall.h"
7c1d2758 55#include "agent.h"
93ec662e 56#include "buffer-registry.h"
b0880ae5
JG
57#include "notification-thread.h"
58#include "notification-thread-commands.h"
5c408ad8
JD
59#include "rotate.h"
60#include "rotation-thread.h"
8e319828 61#include "timer.h"
f28f9e44 62#include "agent-thread.h"
2f77fc4b
DG
63
64#include "cmd.h"
65
a503e1ef
JG
66/* Sleep for 100ms between each check for the shm path's deletion. */
67#define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
68
3e3665b8
JG
69struct cmd_destroy_session_reply_context {
70 int reply_sock_fd;
71 bool implicit_rotation_on_destroy;
3285a971
JG
72 /*
73 * Indicates whether or not an error occurred while launching the
74 * destruction of a session.
75 */
76 enum lttng_error_code destruction_status;
3e3665b8
JG
77};
78
a503e1ef
JG
79static enum lttng_error_code wait_on_path(void *path);
80
81/*
82 * Command completion handler that is used by the destroy command
83 * when a session that has a non-default shm_path is being destroyed.
84 *
85 * See comment in cmd_destroy_session() for the rationale.
86 */
87static struct destroy_completion_handler {
88 struct cmd_completion_handler handler;
89 char shm_path[member_sizeof(struct ltt_session, shm_path)];
90} destroy_completion_handler = {
91 .handler = {
92 .run = wait_on_path,
93 .data = destroy_completion_handler.shm_path
94 },
95 .shm_path = { 0 },
96};
97
98static struct cmd_completion_handler *current_completion_handler;
99
2f77fc4b
DG
100/*
101 * Used to keep a unique index for each relayd socket created where this value
102 * is associated with streams on the consumer so it can match the right relayd
d88aee68
DG
103 * to send to. It must be accessed with the relayd_net_seq_idx_lock
104 * held.
2f77fc4b 105 */
d88aee68
DG
106static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
107static uint64_t relayd_net_seq_idx;
2f77fc4b 108
7076b56e
JG
109static int validate_ust_event_name(const char *);
110static int cmd_enable_event_internal(struct ltt_session *session,
df4f5a87 111 const struct lttng_domain *domain,
7076b56e
JG
112 char *channel_name, struct lttng_event *event,
113 char *filter_expression,
114 struct lttng_filter_bytecode *filter,
115 struct lttng_event_exclusion *exclusion,
116 int wpipe);
117
2f77fc4b
DG
118/*
119 * Create a session path used by list_lttng_sessions for the case that the
120 * session consumer is on the network.
121 */
122static int build_network_session_path(char *dst, size_t size,
123 struct ltt_session *session)
124{
125 int ret, kdata_port, udata_port;
126 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
127 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
128
129 assert(session);
130 assert(dst);
131
132 memset(tmp_urls, 0, sizeof(tmp_urls));
133 memset(tmp_uurl, 0, sizeof(tmp_uurl));
134
135 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
136
137 if (session->kernel_session && session->kernel_session->consumer) {
138 kuri = &session->kernel_session->consumer->dst.net.control;
139 kdata_port = session->kernel_session->consumer->dst.net.data.port;
140 }
141
142 if (session->ust_session && session->ust_session->consumer) {
143 uuri = &session->ust_session->consumer->dst.net.control;
144 udata_port = session->ust_session->consumer->dst.net.data.port;
145 }
146
147 if (uuri == NULL && kuri == NULL) {
148 uri = &session->consumer->dst.net.control;
149 kdata_port = session->consumer->dst.net.data.port;
150 } else if (kuri && uuri) {
151 ret = uri_compare(kuri, uuri);
152 if (ret) {
153 /* Not Equal */
154 uri = kuri;
155 /* Build uuri URL string */
156 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
157 if (ret < 0) {
158 goto error;
159 }
160 } else {
161 uri = kuri;
162 }
163 } else if (kuri && uuri == NULL) {
164 uri = kuri;
165 } else if (uuri && kuri == NULL) {
166 uri = uuri;
167 }
168
169 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
170 if (ret < 0) {
171 goto error;
172 }
173
9aa9f900
DG
174 /*
175 * Do we have a UST url set. If yes, this means we have both kernel and UST
176 * to print.
177 */
9d035200 178 if (*tmp_uurl != '\0') {
2f77fc4b
DG
179 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
180 tmp_urls, kdata_port, tmp_uurl, udata_port);
181 } else {
9aa9f900 182 int dport;
bef08707 183 if (kuri || (!kuri && !uuri)) {
9aa9f900
DG
184 dport = kdata_port;
185 } else {
186 /* No kernel URI, use the UST port. */
187 dport = udata_port;
188 }
189 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
2f77fc4b
DG
190 }
191
192error:
193 return ret;
194}
195
fb83fe64
JD
196/*
197 * Get run-time attributes if the session has been started (discarded events,
198 * lost packets).
199 */
200static int get_kernel_runtime_stats(struct ltt_session *session,
201 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
202 uint64_t *lost_packets)
203{
204 int ret;
205
206 if (!session->has_been_started) {
207 ret = 0;
208 *discarded_events = 0;
209 *lost_packets = 0;
210 goto end;
211 }
212
e1f3997a 213 ret = consumer_get_discarded_events(session->id, kchan->key,
fb83fe64
JD
214 session->kernel_session->consumer,
215 discarded_events);
216 if (ret < 0) {
217 goto end;
218 }
219
e1f3997a 220 ret = consumer_get_lost_packets(session->id, kchan->key,
fb83fe64
JD
221 session->kernel_session->consumer,
222 lost_packets);
223 if (ret < 0) {
224 goto end;
225 }
226
227end:
228 return ret;
229}
230
231/*
232 * Get run-time attributes if the session has been started (discarded events,
233 * lost packets).
234 */
235static int get_ust_runtime_stats(struct ltt_session *session,
236 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
237 uint64_t *lost_packets)
238{
239 int ret;
240 struct ltt_ust_session *usess;
241
a91c5803
JG
242 if (!discarded_events || !lost_packets) {
243 ret = -1;
244 goto end;
245 }
246
fb83fe64 247 usess = session->ust_session;
a905c624
JG
248 assert(discarded_events);
249 assert(lost_packets);
fb83fe64
JD
250
251 if (!usess || !session->has_been_started) {
252 *discarded_events = 0;
253 *lost_packets = 0;
254 ret = 0;
255 goto end;
256 }
257
258 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
259 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
260 &usess->buffer_reg_uid_list,
261 usess->consumer, uchan->id,
262 uchan->attr.overwrite,
263 discarded_events,
264 lost_packets);
265 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
266 ret = ust_app_pid_get_channel_runtime_stats(usess,
267 uchan, usess->consumer,
268 uchan->attr.overwrite,
269 discarded_events,
270 lost_packets);
271 if (ret < 0) {
272 goto end;
273 }
274 *discarded_events += uchan->per_pid_closed_app_discarded;
275 *lost_packets += uchan->per_pid_closed_app_lost;
276 } else {
277 ERR("Unsupported buffer type");
967bc289 278 assert(0);
fb83fe64
JD
279 ret = -1;
280 goto end;
281 }
282
283end:
284 return ret;
285}
286
2f77fc4b
DG
287/*
288 * Fill lttng_channel array of all channels.
289 */
d449df4a 290static ssize_t list_lttng_channels(enum lttng_domain_type domain,
fb83fe64 291 struct ltt_session *session, struct lttng_channel *channels,
cf0bcb51 292 struct lttng_channel_extended *chan_exts)
2f77fc4b 293{
d449df4a 294 int i = 0, ret = 0;
2f77fc4b
DG
295 struct ltt_kernel_channel *kchan;
296
297 DBG("Listing channels for session %s", session->name);
298
299 switch (domain) {
300 case LTTNG_DOMAIN_KERNEL:
301 /* Kernel channels */
302 if (session->kernel_session != NULL) {
303 cds_list_for_each_entry(kchan,
304 &session->kernel_session->channel_list.head, list) {
fb83fe64 305 uint64_t discarded_events, lost_packets;
cf0bcb51
JG
306 struct lttng_channel_extended *extended;
307
308 extended = (struct lttng_channel_extended *)
309 kchan->channel->attr.extended.ptr;
fb83fe64
JD
310
311 ret = get_kernel_runtime_stats(session, kchan,
312 &discarded_events, &lost_packets);
313 if (ret < 0) {
314 goto end;
315 }
2f77fc4b
DG
316 /* Copy lttng_channel struct to array */
317 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
318 channels[i].enabled = kchan->enabled;
fb83fe64
JD
319 chan_exts[i].discarded_events =
320 discarded_events;
321 chan_exts[i].lost_packets = lost_packets;
cf0bcb51
JG
322 chan_exts[i].monitor_timer_interval =
323 extended->monitor_timer_interval;
491d1539 324 chan_exts[i].blocking_timeout = 0;
2f77fc4b
DG
325 i++;
326 }
327 }
328 break;
329 case LTTNG_DOMAIN_UST:
330 {
331 struct lttng_ht_iter iter;
332 struct ltt_ust_channel *uchan;
333
e7fe706f 334 rcu_read_lock();
2f77fc4b
DG
335 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
336 &iter.iter, uchan, node.node) {
a91c5803 337 uint64_t discarded_events = 0, lost_packets = 0;
fb83fe64 338
8ee609c8
MD
339 if (lttng_strncpy(channels[i].name, uchan->name,
340 LTTNG_SYMBOL_NAME_LEN)) {
341 break;
342 }
2f77fc4b
DG
343 channels[i].attr.overwrite = uchan->attr.overwrite;
344 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
345 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
346 channels[i].attr.switch_timer_interval =
347 uchan->attr.switch_timer_interval;
348 channels[i].attr.read_timer_interval =
349 uchan->attr.read_timer_interval;
350 channels[i].enabled = uchan->enabled;
2f785fe7
DG
351 channels[i].attr.tracefile_size = uchan->tracefile_size;
352 channels[i].attr.tracefile_count = uchan->tracefile_count;
5e4435a4
JG
353
354 /*
355 * Map enum lttng_ust_output to enum lttng_event_output.
356 */
2f77fc4b
DG
357 switch (uchan->attr.output) {
358 case LTTNG_UST_MMAP:
2f77fc4b
DG
359 channels[i].attr.output = LTTNG_EVENT_MMAP;
360 break;
5e4435a4
JG
361 default:
362 /*
363 * LTTNG_UST_MMAP is the only supported UST
364 * output mode.
365 */
366 assert(0);
367 break;
2f77fc4b 368 }
fb83fe64 369
d449df4a
MD
370 chan_exts[i].monitor_timer_interval =
371 uchan->monitor_timer_interval;
491d1539
MD
372 chan_exts[i].blocking_timeout =
373 uchan->attr.u.s.blocking_timeout;
d449df4a 374
fb83fe64
JD
375 ret = get_ust_runtime_stats(session, uchan,
376 &discarded_events, &lost_packets);
377 if (ret < 0) {
378 break;
379 }
380 chan_exts[i].discarded_events = discarded_events;
381 chan_exts[i].lost_packets = lost_packets;
2f77fc4b
DG
382 i++;
383 }
e7fe706f 384 rcu_read_unlock();
2f77fc4b
DG
385 break;
386 }
387 default:
388 break;
389 }
fb83fe64
JD
390
391end:
d449df4a
MD
392 if (ret < 0) {
393 return -LTTNG_ERR_FATAL;
394 } else {
395 return LTTNG_OK;
396 }
2f77fc4b
DG
397}
398
3c02e545
FD
399static int increment_extended_len(const char *filter_expression,
400 struct lttng_event_exclusion *exclusion,
401 const struct lttng_userspace_probe_location *probe_location,
402 size_t *extended_len)
b4e3ceb9 403{
3c02e545
FD
404 int ret = 0;
405
b4e3ceb9
PP
406 *extended_len += sizeof(struct lttcomm_event_extended_header);
407
408 if (filter_expression) {
409 *extended_len += strlen(filter_expression) + 1;
410 }
795d57ce
PP
411
412 if (exclusion) {
413 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
414 }
3c02e545
FD
415
416 if (probe_location) {
417 ret = lttng_userspace_probe_location_serialize(probe_location,
418 NULL, NULL);
419 if (ret < 0) {
420 goto end;
421 }
422 *extended_len += ret;
423 }
424 ret = 0;
425end:
426 return ret;
b4e3ceb9
PP
427}
428
3c02e545
FD
429static int append_extended_info(const char *filter_expression,
430 struct lttng_event_exclusion *exclusion,
431 struct lttng_userspace_probe_location *probe_location,
432 void **extended_at)
b4e3ceb9 433{
3c02e545 434 int ret = 0;
b4e3ceb9 435 size_t filter_len = 0;
795d57ce 436 size_t nb_exclusions = 0;
3c02e545
FD
437 size_t userspace_probe_location_len = 0;
438 struct lttng_dynamic_buffer location_buffer;
439 struct lttcomm_event_extended_header extended_header;
b4e3ceb9
PP
440
441 if (filter_expression) {
442 filter_len = strlen(filter_expression) + 1;
443 }
444
795d57ce
PP
445 if (exclusion) {
446 nb_exclusions = exclusion->count;
447 }
448
3c02e545
FD
449 if (probe_location) {
450 lttng_dynamic_buffer_init(&location_buffer);
451 ret = lttng_userspace_probe_location_serialize(probe_location,
452 &location_buffer, NULL);
453 if (ret < 0) {
454 ret = -1;
455 goto end;
456 }
457 userspace_probe_location_len = location_buffer.size;
458 }
459
795d57ce 460 /* Set header fields */
b4e3ceb9 461 extended_header.filter_len = filter_len;
795d57ce 462 extended_header.nb_exclusions = nb_exclusions;
3c02e545 463 extended_header.userspace_probe_location_len = userspace_probe_location_len;
795d57ce
PP
464
465 /* Copy header */
b4e3ceb9
PP
466 memcpy(*extended_at, &extended_header, sizeof(extended_header));
467 *extended_at += sizeof(extended_header);
795d57ce
PP
468
469 /* Copy filter string */
470 if (filter_expression) {
471 memcpy(*extended_at, filter_expression, filter_len);
472 *extended_at += filter_len;
473 }
474
475 /* Copy exclusion names */
476 if (exclusion) {
477 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
478
479 memcpy(*extended_at, &exclusion->names, len);
480 *extended_at += len;
481 }
3c02e545
FD
482
483 if (probe_location) {
484 memcpy(*extended_at, location_buffer.data, location_buffer.size);
485 *extended_at += location_buffer.size;
486 lttng_dynamic_buffer_reset(&location_buffer);
487 }
488 ret = 0;
489end:
490 return ret;
b4e3ceb9
PP
491}
492
3c6a091f 493/*
022d91ba 494 * Create a list of agent domain events.
3c6a091f
DG
495 *
496 * Return number of events in list on success or else a negative value.
497 */
022d91ba 498static int list_lttng_agent_events(struct agent *agt,
b4e3ceb9 499 struct lttng_event **events, size_t *total_size)
3c6a091f
DG
500{
501 int i = 0, ret = 0;
502 unsigned int nb_event = 0;
022d91ba 503 struct agent_event *event;
47e52862 504 struct lttng_event *tmp_events = NULL;
3c6a091f 505 struct lttng_ht_iter iter;
b4e3ceb9
PP
506 size_t extended_len = 0;
507 void *extended_at;
3c6a091f 508
022d91ba 509 assert(agt);
3c6a091f
DG
510 assert(events);
511
022d91ba 512 DBG3("Listing agent events");
3c6a091f 513
e5bbf678 514 rcu_read_lock();
022d91ba 515 nb_event = lttng_ht_get_count(agt->events);
e5bbf678 516 rcu_read_unlock();
3c6a091f
DG
517 if (nb_event == 0) {
518 ret = nb_event;
b4e3ceb9 519 *total_size = 0;
3c6a091f
DG
520 goto error;
521 }
522
b4e3ceb9
PP
523 /* Compute required extended infos size */
524 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
525
526 /*
527 * This is only valid because the commands which add events are
528 * processed in the same thread as the listing.
529 */
530 rcu_read_lock();
531 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
3c02e545 532 ret = increment_extended_len(event->filter_expression, NULL, NULL,
795d57ce 533 &extended_len);
3c02e545
FD
534 if (ret) {
535 DBG("Error computing the length of extended info message");
536 ret = -LTTNG_ERR_FATAL;
537 goto error;
538 }
b4e3ceb9
PP
539 }
540 rcu_read_unlock();
541
542 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
543 tmp_events = zmalloc(*total_size);
3c6a091f 544 if (!tmp_events) {
022d91ba 545 PERROR("zmalloc agent events session");
3c6a091f
DG
546 ret = -LTTNG_ERR_FATAL;
547 goto error;
548 }
549
b4e3ceb9
PP
550 extended_at = ((uint8_t *) tmp_events) +
551 nb_event * sizeof(struct lttng_event);
552
3c6a091f 553 rcu_read_lock();
022d91ba 554 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
3c6a091f
DG
555 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
556 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
557 tmp_events[i].enabled = event->enabled;
2106efa0 558 tmp_events[i].loglevel = event->loglevel_value;
ff94328f 559 tmp_events[i].loglevel_type = event->loglevel_type;
3c6a091f 560 i++;
b4e3ceb9
PP
561
562 /* Append extended info */
3c02e545 563 ret = append_extended_info(event->filter_expression, NULL, NULL,
795d57ce 564 &extended_at);
3c02e545
FD
565 if (ret) {
566 DBG("Error appending extended info message");
567 ret = -LTTNG_ERR_FATAL;
568 goto error;
569 }
3c6a091f 570 }
3c6a091f
DG
571
572 *events = tmp_events;
573 ret = nb_event;
3c02e545 574 assert(nb_event == i);
3c6a091f 575
47e52862 576end:
3c02e545 577 rcu_read_unlock();
3c6a091f 578 return ret;
47e52862
JG
579error:
580 free(tmp_events);
581 goto end;
3c6a091f
DG
582}
583
2f77fc4b
DG
584/*
585 * Create a list of ust global domain events.
586 */
587static int list_lttng_ust_global_events(char *channel_name,
b4e3ceb9
PP
588 struct ltt_ust_domain_global *ust_global,
589 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
590{
591 int i = 0, ret = 0;
592 unsigned int nb_event = 0;
593 struct lttng_ht_iter iter;
594 struct lttng_ht_node_str *node;
595 struct ltt_ust_channel *uchan;
596 struct ltt_ust_event *uevent;
597 struct lttng_event *tmp;
b4e3ceb9
PP
598 size_t extended_len = 0;
599 void *extended_at;
2f77fc4b
DG
600
601 DBG("Listing UST global events for channel %s", channel_name);
602
603 rcu_read_lock();
604
605 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
606 node = lttng_ht_iter_get_node_str(&iter);
607 if (node == NULL) {
f73fabfd 608 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
d31d3e8c 609 goto end;
2f77fc4b
DG
610 }
611
612 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
613
3c442be3 614 nb_event = lttng_ht_get_count(uchan->events);
2f77fc4b
DG
615 if (nb_event == 0) {
616 ret = nb_event;
b4e3ceb9 617 *total_size = 0;
d31d3e8c 618 goto end;
2f77fc4b
DG
619 }
620
621 DBG3("Listing UST global %d events", nb_event);
622
b4e3ceb9
PP
623 /* Compute required extended infos size */
624 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
625 if (uevent->internal) {
626 nb_event--;
627 continue;
628 }
629
3c02e545
FD
630 ret = increment_extended_len(uevent->filter_expression,
631 uevent->exclusion, NULL, &extended_len);
632 if (ret) {
633 DBG("Error computing the length of extended info message");
634 ret = -LTTNG_ERR_FATAL;
635 goto end;
636 }
b4e3ceb9 637 }
d31d3e8c
PP
638 if (nb_event == 0) {
639 /* All events are internal, skip. */
640 ret = 0;
641 *total_size = 0;
642 goto end;
643 }
b4e3ceb9
PP
644
645 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
646 tmp = zmalloc(*total_size);
2f77fc4b 647 if (tmp == NULL) {
b12c38df
JG
648 ret = -LTTNG_ERR_FATAL;
649 goto end;
2f77fc4b
DG
650 }
651
b4e3ceb9
PP
652 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
653
2f77fc4b 654 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
3c442be3
JG
655 if (uevent->internal) {
656 /* This event should remain hidden from clients */
3c442be3
JG
657 continue;
658 }
2f77fc4b
DG
659 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
660 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
661 tmp[i].enabled = uevent->enabled;
662
663 switch (uevent->attr.instrumentation) {
664 case LTTNG_UST_TRACEPOINT:
665 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
666 break;
667 case LTTNG_UST_PROBE:
668 tmp[i].type = LTTNG_EVENT_PROBE;
669 break;
670 case LTTNG_UST_FUNCTION:
671 tmp[i].type = LTTNG_EVENT_FUNCTION;
672 break;
673 }
674
675 tmp[i].loglevel = uevent->attr.loglevel;
676 switch (uevent->attr.loglevel_type) {
677 case LTTNG_UST_LOGLEVEL_ALL:
678 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
679 break;
680 case LTTNG_UST_LOGLEVEL_RANGE:
681 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
682 break;
683 case LTTNG_UST_LOGLEVEL_SINGLE:
684 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
685 break;
686 }
687 if (uevent->filter) {
688 tmp[i].filter = 1;
689 }
4634f12e
JI
690 if (uevent->exclusion) {
691 tmp[i].exclusion = 1;
692 }
2f77fc4b 693 i++;
b4e3ceb9
PP
694
695 /* Append extended info */
3c02e545
FD
696 ret = append_extended_info(uevent->filter_expression,
697 uevent->exclusion, NULL, &extended_at);
698 if (ret) {
699 DBG("Error appending extended info message");
700 ret = -LTTNG_ERR_FATAL;
701 goto end;
702 }
2f77fc4b
DG
703 }
704
705 ret = nb_event;
706 *events = tmp;
d31d3e8c 707end:
2f77fc4b
DG
708 rcu_read_unlock();
709 return ret;
710}
711
712/*
713 * Fill lttng_event array of all kernel events in the channel.
714 */
715static int list_lttng_kernel_events(char *channel_name,
b4e3ceb9
PP
716 struct ltt_kernel_session *kernel_session,
717 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
718{
719 int i = 0, ret;
720 unsigned int nb_event;
721 struct ltt_kernel_event *event;
722 struct ltt_kernel_channel *kchan;
b4e3ceb9
PP
723 size_t extended_len = 0;
724 void *extended_at;
2f77fc4b
DG
725
726 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
727 if (kchan == NULL) {
f73fabfd 728 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
729 goto error;
730 }
731
732 nb_event = kchan->event_count;
733
734 DBG("Listing events for channel %s", kchan->channel->name);
735
736 if (nb_event == 0) {
b4e3ceb9 737 *total_size = 0;
834978fd 738 *events = NULL;
db906c12 739 goto end;
2f77fc4b
DG
740 }
741
b4e3ceb9
PP
742 /* Compute required extended infos size */
743 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
3c02e545
FD
744 ret = increment_extended_len(event->filter_expression, NULL,
745 event->userspace_probe_location,
795d57ce 746 &extended_len);
3c02e545
FD
747 if (ret) {
748 DBG("Error computing the length of extended info message");
749 ret = -LTTNG_ERR_FATAL;
750 goto error;
751 }
b4e3ceb9
PP
752 }
753
754 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
755 *events = zmalloc(*total_size);
2f77fc4b 756 if (*events == NULL) {
3c02e545 757 ret = -LTTNG_ERR_FATAL;
2f77fc4b
DG
758 goto error;
759 }
760
ab4aa612 761 extended_at = ((void *) *events) +
b4e3ceb9
PP
762 nb_event * sizeof(struct lttng_event);
763
2f77fc4b
DG
764 /* Kernel channels */
765 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
766 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
767 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
768 (*events)[i].enabled = event->enabled;
00f992f2
JG
769 (*events)[i].filter =
770 (unsigned char) !!event->filter_expression;
2f77fc4b
DG
771
772 switch (event->event->instrumentation) {
773 case LTTNG_KERNEL_TRACEPOINT:
774 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
775 break;
2f77fc4b 776 case LTTNG_KERNEL_KRETPROBE:
1896972b
DG
777 (*events)[i].type = LTTNG_EVENT_FUNCTION;
778 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
779 sizeof(struct lttng_kernel_kprobe));
780 break;
781 case LTTNG_KERNEL_KPROBE:
2f77fc4b
DG
782 (*events)[i].type = LTTNG_EVENT_PROBE;
783 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
784 sizeof(struct lttng_kernel_kprobe));
785 break;
b955b4d4
FD
786 case LTTNG_KERNEL_UPROBE:
787 (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE;
788 break;
2f77fc4b
DG
789 case LTTNG_KERNEL_FUNCTION:
790 (*events)[i].type = LTTNG_EVENT_FUNCTION;
791 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
792 sizeof(struct lttng_kernel_function));
793 break;
794 case LTTNG_KERNEL_NOOP:
795 (*events)[i].type = LTTNG_EVENT_NOOP;
796 break;
797 case LTTNG_KERNEL_SYSCALL:
798 (*events)[i].type = LTTNG_EVENT_SYSCALL;
799 break;
800 case LTTNG_KERNEL_ALL:
1ab8c2ad
FD
801 /* fall-through. */
802 default:
2f77fc4b
DG
803 assert(0);
804 break;
805 }
806 i++;
b4e3ceb9
PP
807
808 /* Append extended info */
3c02e545
FD
809 ret = append_extended_info(event->filter_expression, NULL,
810 event->userspace_probe_location, &extended_at);
811 if (ret) {
812 DBG("Error appending extended info message");
813 ret = -LTTNG_ERR_FATAL;
814 goto error;
815 }
2f77fc4b
DG
816 }
817
db906c12 818end:
2f77fc4b
DG
819 return nb_event;
820
821error:
6314496f 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));
4df41cad
JG
915 if (ret) {
916 ret_code = LTTNG_ERR_FATAL;
917 goto error;
918 }
2f77fc4b
DG
919 consumer->type = CONSUMER_DST_LOCAL;
920 break;
921 }
922
b178f53e 923 ret_code = LTTNG_OK;
2f77fc4b 924error:
b178f53e 925 return ret_code;
2f77fc4b
DG
926}
927
928/*
929 * Init tracing by creating trace directory and sending fds kernel consumer.
930 */
931static int init_kernel_tracing(struct ltt_kernel_session *session)
932{
933 int ret = 0;
934 struct lttng_ht_iter iter;
935 struct consumer_socket *socket;
936
937 assert(session);
938
e7fe706f
DG
939 rcu_read_lock();
940
2f77fc4b
DG
941 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
942 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
943 socket, node.node) {
2f77fc4b 944 pthread_mutex_lock(socket->lock);
f50f23d9 945 ret = kernel_consumer_send_session(socket, session);
2f77fc4b
DG
946 pthread_mutex_unlock(socket->lock);
947 if (ret < 0) {
f73fabfd 948 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
949 goto error;
950 }
951 }
952 }
953
954error:
e7fe706f 955 rcu_read_unlock();
2f77fc4b
DG
956 return ret;
957}
958
959/*
960 * Create a socket to the relayd using the URI.
961 *
962 * On success, the relayd_sock pointer is set to the created socket.
9a654598 963 * Else, it remains untouched and an LTTng error code is returned.
2f77fc4b 964 */
9a654598 965static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
b31610f2
JD
966 struct lttcomm_relayd_sock **relayd_sock,
967 struct consumer_output *consumer)
2f77fc4b
DG
968{
969 int ret;
9a654598 970 enum lttng_error_code status = LTTNG_OK;
6151a90f 971 struct lttcomm_relayd_sock *rsock;
2f77fc4b 972
6151a90f
JD
973 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
974 RELAYD_VERSION_COMM_MINOR);
975 if (!rsock) {
9a654598 976 status = LTTNG_ERR_FATAL;
2f77fc4b
DG
977 goto error;
978 }
979
ffe60014
DG
980 /*
981 * Connect to relayd so we can proceed with a session creation. This call
982 * can possibly block for an arbitrary amount of time to set the health
983 * state to be in poll execution.
984 */
985 health_poll_entry();
6151a90f 986 ret = relayd_connect(rsock);
ffe60014 987 health_poll_exit();
2f77fc4b
DG
988 if (ret < 0) {
989 ERR("Unable to reach lttng-relayd");
9a654598 990 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
991 goto free_sock;
992 }
993
994 /* Create socket for control stream. */
995 if (uri->stype == LTTNG_STREAM_CONTROL) {
eacb7b6f
MD
996 uint64_t result_flags;
997
2f77fc4b
DG
998 DBG3("Creating relayd stream socket from URI");
999
1000 /* Check relayd version */
6151a90f 1001 ret = relayd_version_check(rsock);
67d5aa28 1002 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
9a654598 1003 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
67d5aa28
JD
1004 goto close_sock;
1005 } else if (ret < 0) {
1006 ERR("Unable to reach lttng-relayd");
9a654598 1007 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
1008 goto close_sock;
1009 }
b31610f2
JD
1010 consumer->relay_major_version = rsock->major;
1011 consumer->relay_minor_version = rsock->minor;
eacb7b6f
MD
1012 ret = relayd_get_configuration(rsock, 0,
1013 &result_flags);
1014 if (ret < 0) {
1015 ERR("Unable to get relayd configuration");
1016 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
1017 goto close_sock;
1018 }
1019 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
1020 consumer->relay_allows_clear = true;
1021 }
2f77fc4b
DG
1022 } else if (uri->stype == LTTNG_STREAM_DATA) {
1023 DBG3("Creating relayd data socket from URI");
1024 } else {
1025 /* Command is not valid */
1026 ERR("Relayd invalid stream type: %d", uri->stype);
9a654598 1027 status = LTTNG_ERR_INVALID;
2f77fc4b
DG
1028 goto close_sock;
1029 }
1030
6151a90f 1031 *relayd_sock = rsock;
2f77fc4b 1032
9a654598 1033 return status;
2f77fc4b
DG
1034
1035close_sock:
6151a90f
JD
1036 /* The returned value is not useful since we are on an error path. */
1037 (void) relayd_close(rsock);
2f77fc4b 1038free_sock:
6151a90f 1039 free(rsock);
2f77fc4b 1040error:
9a654598 1041 return status;
2f77fc4b
DG
1042}
1043
1044/*
1045 * Connect to the relayd using URI and send the socket to the right consumer.
43fade62
JG
1046 *
1047 * The consumer socket lock must be held by the caller.
9a654598
JG
1048 *
1049 * Returns LTTNG_OK on success or an LTTng error code on failure.
2f77fc4b 1050 */
9a654598
JG
1051static enum lttng_error_code send_consumer_relayd_socket(
1052 unsigned int session_id,
3044f922 1053 struct lttng_uri *relayd_uri,
56a37563 1054 struct consumer_output *consumer,
d3e2ba59 1055 struct consumer_socket *consumer_sock,
fb9a95c4 1056 const char *session_name, const char *hostname,
6fa5fe7c 1057 const char *base_path, int session_live_timer,
db1da059 1058 const uint64_t *current_chunk_id,
46ef2188
MD
1059 time_t session_creation_time,
1060 bool session_name_contains_creation_time)
2f77fc4b
DG
1061{
1062 int ret;
6151a90f 1063 struct lttcomm_relayd_sock *rsock = NULL;
9a654598 1064 enum lttng_error_code status;
2f77fc4b 1065
ffe60014 1066 /* Connect to relayd and make version check if uri is the control. */
9a654598
JG
1067 status = create_connect_relayd(relayd_uri, &rsock, consumer);
1068 if (status != LTTNG_OK) {
9e218353 1069 goto relayd_comm_error;
ffe60014 1070 }
6151a90f 1071 assert(rsock);
ffe60014 1072
2f77fc4b 1073 /* Set the network sequence index if not set. */
d88aee68
DG
1074 if (consumer->net_seq_index == (uint64_t) -1ULL) {
1075 pthread_mutex_lock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
1076 /*
1077 * Increment net_seq_idx because we are about to transfer the
1078 * new relayd socket to the consumer.
d88aee68 1079 * Assign unique key so the consumer can match streams.
2f77fc4b 1080 */
d88aee68
DG
1081 consumer->net_seq_index = ++relayd_net_seq_idx;
1082 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
1083 }
1084
2f77fc4b 1085 /* Send relayd socket to consumer. */
6151a90f 1086 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
d3e2ba59 1087 relayd_uri->stype, session_id,
6fa5fe7c
MD
1088 session_name, hostname, base_path,
1089 session_live_timer, current_chunk_id,
46ef2188 1090 session_creation_time, session_name_contains_creation_time);
2f77fc4b 1091 if (ret < 0) {
9a654598 1092 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2f77fc4b
DG
1093 goto close_sock;
1094 }
1095
c890b720
DG
1096 /* Flag that the corresponding socket was sent. */
1097 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
ffe60014 1098 consumer_sock->control_sock_sent = 1;
c890b720 1099 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
ffe60014 1100 consumer_sock->data_sock_sent = 1;
c890b720
DG
1101 }
1102
2f77fc4b
DG
1103 /*
1104 * Close socket which was dup on the consumer side. The session daemon does
1105 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1106 */
1107
1108close_sock:
9a654598 1109 if (status != LTTNG_OK) {
ffe60014 1110 /*
d9078d0c
DG
1111 * The consumer output for this session should not be used anymore
1112 * since the relayd connection failed thus making any tracing or/and
1113 * streaming not usable.
ffe60014 1114 */
d9078d0c 1115 consumer->enabled = 0;
ffe60014 1116 }
9e218353
JR
1117 (void) relayd_close(rsock);
1118 free(rsock);
1119
1120relayd_comm_error:
9a654598 1121 return status;
2f77fc4b
DG
1122}
1123
1124/*
1125 * Send both relayd sockets to a specific consumer and domain. This is a
1126 * helper function to facilitate sending the information to the consumer for a
1127 * session.
43fade62
JG
1128 *
1129 * The consumer socket lock must be held by the caller.
9a654598
JG
1130 *
1131 * Returns LTTNG_OK, or an LTTng error code on failure.
2f77fc4b 1132 */
9a654598
JG
1133static enum lttng_error_code send_consumer_relayd_sockets(
1134 enum lttng_domain_type domain,
56a37563 1135 unsigned int session_id, struct consumer_output *consumer,
fb9a95c4 1136 struct consumer_socket *sock, const char *session_name,
6fa5fe7c 1137 const char *hostname, const char *base_path, int session_live_timer,
46ef2188
MD
1138 const uint64_t *current_chunk_id, time_t session_creation_time,
1139 bool session_name_contains_creation_time)
2f77fc4b 1140{
9a654598 1141 enum lttng_error_code status = LTTNG_OK;
2f77fc4b 1142
2f77fc4b 1143 assert(consumer);
6dc3064a 1144 assert(sock);
2f77fc4b 1145
2f77fc4b 1146 /* Sending control relayd socket. */
ffe60014 1147 if (!sock->control_sock_sent) {
9a654598 1148 status = send_consumer_relayd_socket(session_id,
d3e2ba59 1149 &consumer->dst.net.control, consumer, sock,
6fa5fe7c 1150 session_name, hostname, base_path, session_live_timer,
46ef2188
MD
1151 current_chunk_id, session_creation_time,
1152 session_name_contains_creation_time);
9a654598 1153 if (status != LTTNG_OK) {
c890b720
DG
1154 goto error;
1155 }
2f77fc4b
DG
1156 }
1157
1158 /* Sending data relayd socket. */
ffe60014 1159 if (!sock->data_sock_sent) {
9a654598 1160 status = send_consumer_relayd_socket(session_id,
d3e2ba59 1161 &consumer->dst.net.data, consumer, sock,
6fa5fe7c 1162 session_name, hostname, base_path, session_live_timer,
46ef2188
MD
1163 current_chunk_id, session_creation_time,
1164 session_name_contains_creation_time);
9a654598 1165 if (status != LTTNG_OK) {
c890b720
DG
1166 goto error;
1167 }
2f77fc4b
DG
1168 }
1169
2f77fc4b 1170error:
9a654598 1171 return status;
2f77fc4b
DG
1172}
1173
1174/*
1175 * Setup relayd connections for a tracing session. First creates the socket to
1176 * the relayd and send them to the right domain consumer. Consumer type MUST be
1177 * network.
1178 */
ffe60014 1179int cmd_setup_relayd(struct ltt_session *session)
2f77fc4b 1180{
f73fabfd 1181 int ret = LTTNG_OK;
2f77fc4b
DG
1182 struct ltt_ust_session *usess;
1183 struct ltt_kernel_session *ksess;
1184 struct consumer_socket *socket;
1185 struct lttng_ht_iter iter;
1e791a74 1186 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
2f77fc4b 1187
1e791a74 1188 assert(session);
2f77fc4b
DG
1189
1190 usess = session->ust_session;
1191 ksess = session->kernel_session;
1192
785d2d0d 1193 DBG("Setting relayd for session %s", session->name);
2f77fc4b 1194
aa997ea3 1195 rcu_read_lock();
1e791a74
JG
1196 if (session->current_trace_chunk) {
1197 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1198 session->current_trace_chunk, &current_chunk_id.value);
1199
1200 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1201 current_chunk_id.is_set = true;
1202 } else {
1203 ERR("Failed to get current trace chunk id");
1204 ret = LTTNG_ERR_UNK;
1205 goto error;
1206 }
1207 }
1208
2f77fc4b
DG
1209 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1210 && usess->consumer->enabled) {
1211 /* For each consumer socket, send relayd sockets */
1212 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1213 socket, node.node) {
2f77fc4b 1214 pthread_mutex_lock(socket->lock);
6dc3064a 1215 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
d3e2ba59
JD
1216 usess->consumer, socket,
1217 session->name, session->hostname,
6fa5fe7c 1218 session->base_path,
1e791a74 1219 session->live_timer,
db1da059 1220 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
46ef2188
MD
1221 session->creation_time,
1222 session->name_contains_creation_time);
2f77fc4b 1223 pthread_mutex_unlock(socket->lock);
f73fabfd 1224 if (ret != LTTNG_OK) {
2f77fc4b
DG
1225 goto error;
1226 }
6dc3064a
DG
1227 /* Session is now ready for network streaming. */
1228 session->net_handle = 1;
2f77fc4b 1229 }
b31610f2
JD
1230 session->consumer->relay_major_version =
1231 usess->consumer->relay_major_version;
1232 session->consumer->relay_minor_version =
1233 usess->consumer->relay_minor_version;
eacb7b6f
MD
1234 session->consumer->relay_allows_clear =
1235 usess->consumer->relay_allows_clear;
2f77fc4b
DG
1236 }
1237
1238 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1239 && ksess->consumer->enabled) {
1240 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1241 socket, node.node) {
2f77fc4b 1242 pthread_mutex_lock(socket->lock);
6dc3064a 1243 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
d3e2ba59
JD
1244 ksess->consumer, socket,
1245 session->name, session->hostname,
6fa5fe7c 1246 session->base_path,
1e791a74 1247 session->live_timer,
db1da059 1248 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
46ef2188
MD
1249 session->creation_time,
1250 session->name_contains_creation_time);
2f77fc4b 1251 pthread_mutex_unlock(socket->lock);
f73fabfd 1252 if (ret != LTTNG_OK) {
2f77fc4b
DG
1253 goto error;
1254 }
6dc3064a
DG
1255 /* Session is now ready for network streaming. */
1256 session->net_handle = 1;
2f77fc4b 1257 }
b31610f2
JD
1258 session->consumer->relay_major_version =
1259 ksess->consumer->relay_major_version;
1260 session->consumer->relay_minor_version =
1261 ksess->consumer->relay_minor_version;
eacb7b6f
MD
1262 session->consumer->relay_allows_clear =
1263 ksess->consumer->relay_allows_clear;
2f77fc4b
DG
1264 }
1265
1266error:
e7fe706f 1267 rcu_read_unlock();
2f77fc4b
DG
1268 return ret;
1269}
1270
9b6c7ec5
DG
1271/*
1272 * Start a kernel session by opening all necessary streams.
1273 */
4dbe1875 1274int start_kernel_session(struct ltt_kernel_session *ksess)
9b6c7ec5
DG
1275{
1276 int ret;
1277 struct ltt_kernel_channel *kchan;
1278
1279 /* Open kernel metadata */
07b86b52 1280 if (ksess->metadata == NULL && ksess->output_traces) {
9b6c7ec5
DG
1281 ret = kernel_open_metadata(ksess);
1282 if (ret < 0) {
1283 ret = LTTNG_ERR_KERN_META_FAIL;
1284 goto error;
1285 }
1286 }
1287
1288 /* Open kernel metadata stream */
07b86b52 1289 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
9b6c7ec5
DG
1290 ret = kernel_open_metadata_stream(ksess);
1291 if (ret < 0) {
1292 ERR("Kernel create metadata stream failed");
1293 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1294 goto error;
1295 }
1296 }
1297
1298 /* For each channel */
1299 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1300 if (kchan->stream_count == 0) {
1301 ret = kernel_open_channel_stream(kchan);
1302 if (ret < 0) {
1303 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1304 goto error;
1305 }
1306 /* Update the stream global counter */
1307 ksess->stream_count_global += ret;
1308 }
1309 }
1310
1311 /* Setup kernel consumer socket and send fds to it */
1312 ret = init_kernel_tracing(ksess);
e43c41c5 1313 if (ret != 0) {
9b6c7ec5
DG
1314 ret = LTTNG_ERR_KERN_START_FAIL;
1315 goto error;
1316 }
1317
1318 /* This start the kernel tracing */
1319 ret = kernel_start_session(ksess);
1320 if (ret < 0) {
1321 ret = LTTNG_ERR_KERN_START_FAIL;
1322 goto error;
1323 }
1324
1325 /* Quiescent wait after starting trace */
7d268848 1326 kernel_wait_quiescent();
9b6c7ec5 1327
14fb1ebe 1328 ksess->active = 1;
9b6c7ec5
DG
1329
1330 ret = LTTNG_OK;
1331
1332error:
1333 return ret;
1334}
1335
4dbe1875
MD
1336int stop_kernel_session(struct ltt_kernel_session *ksess)
1337{
1338 struct ltt_kernel_channel *kchan;
1339 bool error_occurred = false;
1340 int ret;
1341
1342 if (!ksess || !ksess->active) {
1343 return LTTNG_OK;
1344 }
1345 DBG("Stopping kernel tracing");
1346
1347 ret = kernel_stop_session(ksess);
1348 if (ret < 0) {
1349 ret = LTTNG_ERR_KERN_STOP_FAIL;
1350 goto error;
1351 }
1352
1353 kernel_wait_quiescent();
1354
1355 /* Flush metadata after stopping (if exists) */
1356 if (ksess->metadata_stream_fd >= 0) {
1357 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1358 if (ret < 0) {
1359 ERR("Kernel metadata flush failed");
1360 error_occurred = true;
1361 }
1362 }
1363
1364 /* Flush all buffers after stopping */
1365 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1366 ret = kernel_flush_buffer(kchan);
1367 if (ret < 0) {
1368 ERR("Kernel flush buffer error");
1369 error_occurred = true;
1370 }
1371 }
1372
1373 ksess->active = 0;
1374 if (error_occurred) {
1375 ret = LTTNG_ERR_UNK;
1376 } else {
1377 ret = LTTNG_OK;
1378 }
1379error:
1380 return ret;
1381}
1382
2f77fc4b
DG
1383/*
1384 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1385 */
56a37563
JG
1386int cmd_disable_channel(struct ltt_session *session,
1387 enum lttng_domain_type domain, char *channel_name)
2f77fc4b
DG
1388{
1389 int ret;
1390 struct ltt_ust_session *usess;
1391
1392 usess = session->ust_session;
1393
2223c96f
DG
1394 rcu_read_lock();
1395
2f77fc4b
DG
1396 switch (domain) {
1397 case LTTNG_DOMAIN_KERNEL:
1398 {
1399 ret = channel_kernel_disable(session->kernel_session,
1400 channel_name);
f73fabfd 1401 if (ret != LTTNG_OK) {
2f77fc4b
DG
1402 goto error;
1403 }
1404
7d268848 1405 kernel_wait_quiescent();
2f77fc4b
DG
1406 break;
1407 }
1408 case LTTNG_DOMAIN_UST:
1409 {
1410 struct ltt_ust_channel *uchan;
1411 struct lttng_ht *chan_ht;
1412
1413 chan_ht = usess->domain_global.channels;
1414
1415 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1416 if (uchan == NULL) {
f73fabfd 1417 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1418 goto error;
1419 }
1420
7972aab2 1421 ret = channel_ust_disable(usess, uchan);
f73fabfd 1422 if (ret != LTTNG_OK) {
2f77fc4b
DG
1423 goto error;
1424 }
1425 break;
1426 }
2f77fc4b 1427 default:
f73fabfd 1428 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1429 goto error;
1430 }
1431
f73fabfd 1432 ret = LTTNG_OK;
2f77fc4b
DG
1433
1434error:
2223c96f 1435 rcu_read_unlock();
2f77fc4b
DG
1436 return ret;
1437}
1438
1439/*
1440 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1441 *
1442 * The wpipe arguments is used as a notifier for the kernel thread.
1443 */
1444int cmd_enable_channel(struct ltt_session *session,
df4f5a87 1445 const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe)
2f77fc4b
DG
1446{
1447 int ret;
1448 struct ltt_ust_session *usess = session->ust_session;
1449 struct lttng_ht *chan_ht;
1f345e94 1450 size_t len;
df4f5a87 1451 struct lttng_channel attr;
2f77fc4b
DG
1452
1453 assert(session);
df4f5a87 1454 assert(_attr);
7972aab2 1455 assert(domain);
2f77fc4b 1456
df4f5a87
JG
1457 attr = *_attr;
1458 len = lttng_strnlen(attr.name, sizeof(attr.name));
1f345e94
PP
1459
1460 /* Validate channel name */
df4f5a87
JG
1461 if (attr.name[0] == '.' ||
1462 memchr(attr.name, '/', len) != NULL) {
1f345e94
PP
1463 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1464 goto end;
1465 }
1466
df4f5a87 1467 DBG("Enabling channel %s for session %s", attr.name, session->name);
2f77fc4b 1468
03b4fdcf
DG
1469 rcu_read_lock();
1470
ecc48a90
JD
1471 /*
1472 * Don't try to enable a channel if the session has been started at
1473 * some point in time before. The tracer does not allow it.
1474 */
8382cf6f 1475 if (session->has_been_started) {
ecc48a90
JD
1476 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1477 goto error;
1478 }
1479
1480 /*
1481 * If the session is a live session, remove the switch timer, the
1482 * live timer does the same thing but sends also synchronisation
1483 * beacons for inactive streams.
1484 */
1485 if (session->live_timer > 0) {
df4f5a87
JG
1486 attr.attr.live_timer_interval = session->live_timer;
1487 attr.attr.switch_timer_interval = 0;
ecc48a90
JD
1488 }
1489
6e21424e
JR
1490 /* Check for feature support */
1491 switch (domain->type) {
1492 case LTTNG_DOMAIN_KERNEL:
1493 {
7d268848 1494 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
6e21424e
JR
1495 /* Sampling position of buffer is not supported */
1496 WARN("Kernel tracer does not support buffer monitoring. "
1497 "Setting the monitor interval timer to 0 "
1498 "(disabled) for channel '%s' of session '%s'",
df4f5a87
JG
1499 attr.name, session->name);
1500 lttng_channel_set_monitor_timer_interval(&attr, 0);
6e21424e
JR
1501 }
1502 break;
1503 }
1504 case LTTNG_DOMAIN_UST:
f28f9e44 1505 break;
6e21424e
JR
1506 case LTTNG_DOMAIN_JUL:
1507 case LTTNG_DOMAIN_LOG4J:
1508 case LTTNG_DOMAIN_PYTHON:
f28f9e44
JG
1509 if (!agent_tracing_is_enabled()) {
1510 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1511 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1512 goto error;
1513 }
6e21424e
JR
1514 break;
1515 default:
1516 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1517 goto error;
1518 }
1519
7972aab2 1520 switch (domain->type) {
2f77fc4b
DG
1521 case LTTNG_DOMAIN_KERNEL:
1522 {
1523 struct ltt_kernel_channel *kchan;
1524
df4f5a87 1525 kchan = trace_kernel_get_channel_by_name(attr.name,
2f77fc4b
DG
1526 session->kernel_session);
1527 if (kchan == NULL) {
54213acc
JG
1528 if (session->snapshot.nb_output > 0 ||
1529 session->snapshot_mode) {
1530 /* Enforce mmap output for snapshot sessions. */
df4f5a87 1531 attr.attr.output = LTTNG_EVENT_MMAP;
54213acc 1532 }
df4f5a87
JG
1533 ret = channel_kernel_create(session->kernel_session, &attr, wpipe);
1534 if (attr.name[0] != '\0') {
85076754
MD
1535 session->kernel_session->has_non_default_channel = 1;
1536 }
2f77fc4b
DG
1537 } else {
1538 ret = channel_kernel_enable(session->kernel_session, kchan);
1539 }
1540
f73fabfd 1541 if (ret != LTTNG_OK) {
2f77fc4b
DG
1542 goto error;
1543 }
1544
7d268848 1545 kernel_wait_quiescent();
2f77fc4b
DG
1546 break;
1547 }
1548 case LTTNG_DOMAIN_UST:
9232818f
JG
1549 case LTTNG_DOMAIN_JUL:
1550 case LTTNG_DOMAIN_LOG4J:
1551 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
1552 {
1553 struct ltt_ust_channel *uchan;
1554
9232818f
JG
1555 /*
1556 * FIXME
1557 *
1558 * Current agent implementation limitations force us to allow
1559 * only one channel at once in "agent" subdomains. Each
1560 * subdomain has a default channel name which must be strictly
1561 * adhered to.
1562 */
1563 if (domain->type == LTTNG_DOMAIN_JUL) {
df4f5a87 1564 if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME,
9232818f
JG
1565 LTTNG_SYMBOL_NAME_LEN)) {
1566 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1567 goto error;
1568 }
1569 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
df4f5a87 1570 if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME,
9232818f
JG
1571 LTTNG_SYMBOL_NAME_LEN)) {
1572 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1573 goto error;
1574 }
1575 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
df4f5a87 1576 if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME,
9232818f
JG
1577 LTTNG_SYMBOL_NAME_LEN)) {
1578 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1579 goto error;
1580 }
1581 }
1582
2f77fc4b
DG
1583 chan_ht = usess->domain_global.channels;
1584
df4f5a87 1585 uchan = trace_ust_find_channel_by_name(chan_ht, attr.name);
2f77fc4b 1586 if (uchan == NULL) {
df4f5a87
JG
1587 ret = channel_ust_create(usess, &attr, domain->buf_type);
1588 if (attr.name[0] != '\0') {
85076754
MD
1589 usess->has_non_default_channel = 1;
1590 }
2f77fc4b 1591 } else {
7972aab2 1592 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1593 }
1594 break;
1595 }
2f77fc4b 1596 default:
f73fabfd 1597 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1598 goto error;
1599 }
1600
df4f5a87 1601 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
54213acc
JG
1602 session->has_non_mmap_channel = true;
1603 }
2f77fc4b 1604error:
2223c96f 1605 rcu_read_unlock();
1f345e94 1606end:
2f77fc4b
DG
1607 return ret;
1608}
1609
159b042f
JG
1610enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1611 struct ltt_session *session,
1612 enum lttng_domain_type domain,
1613 enum lttng_process_attr process_attr,
1614 enum lttng_tracking_policy *policy)
1615{
1616 enum lttng_error_code ret_code = LTTNG_OK;
1617 const struct process_attr_tracker *tracker;
1618
1619 switch (domain) {
1620 case LTTNG_DOMAIN_KERNEL:
1621 if (!session->kernel_session) {
1622 ret_code = LTTNG_ERR_INVALID;
1623 goto end;
1624 }
1625 tracker = kernel_get_process_attr_tracker(
1626 session->kernel_session, process_attr);
1627 break;
1628 case LTTNG_DOMAIN_UST:
1629 if (!session->ust_session) {
1630 ret_code = LTTNG_ERR_INVALID;
1631 goto end;
1632 }
1633 tracker = trace_ust_get_process_attr_tracker(
1634 session->ust_session, process_attr);
1635 break;
1636 default:
1637 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1638 goto end;
1639 }
1640 if (tracker) {
1641 *policy = process_attr_tracker_get_tracking_policy(tracker);
1642 } else {
1643 ret_code = LTTNG_ERR_INVALID;
1644 }
1645end:
1646 return ret_code;
1647}
1648
1649enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1650 struct ltt_session *session,
1651 enum lttng_domain_type domain,
1652 enum lttng_process_attr process_attr,
1653 enum lttng_tracking_policy policy)
1654{
1655 enum lttng_error_code ret_code = LTTNG_OK;
1656
1657 switch (policy) {
1658 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1659 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1660 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1661 break;
1662 default:
1663 ret_code = LTTNG_ERR_INVALID;
1664 goto end;
1665 }
1666
1667 switch (domain) {
1668 case LTTNG_DOMAIN_KERNEL:
1669 if (!session->kernel_session) {
1670 ret_code = LTTNG_ERR_INVALID;
1671 goto end;
1672 }
1673 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1674 session->kernel_session, process_attr, policy);
1675 break;
1676 case LTTNG_DOMAIN_UST:
1677 if (!session->ust_session) {
1678 ret_code = LTTNG_ERR_INVALID;
1679 goto end;
1680 }
1681 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1682 session->ust_session, process_attr, policy);
1683 break;
1684 default:
1685 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1686 break;
1687 }
1688end:
1689 return ret_code;
1690}
1691
1692enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1693 struct ltt_session *session,
1694 enum lttng_domain_type domain,
1695 enum lttng_process_attr process_attr,
1696 const struct process_attr_value *value)
1697{
1698 enum lttng_error_code ret_code = LTTNG_OK;
1699
1700 switch (domain) {
1701 case LTTNG_DOMAIN_KERNEL:
1702 if (!session->kernel_session) {
1703 ret_code = LTTNG_ERR_INVALID;
1704 goto end;
1705 }
1706 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1707 session->kernel_session, process_attr, value);
1708 break;
1709 case LTTNG_DOMAIN_UST:
1710 if (!session->ust_session) {
1711 ret_code = LTTNG_ERR_INVALID;
1712 goto end;
1713 }
1714 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1715 session->ust_session, process_attr, value);
1716 break;
1717 default:
1718 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1719 break;
1720 }
1721end:
1722 return ret_code;
1723}
1724
1725enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1726 struct ltt_session *session,
1727 enum lttng_domain_type domain,
1728 enum lttng_process_attr process_attr,
1729 const struct process_attr_value *value)
1730{
1731 enum lttng_error_code ret_code = LTTNG_OK;
1732
1733 switch (domain) {
1734 case LTTNG_DOMAIN_KERNEL:
1735 if (!session->kernel_session) {
1736 ret_code = LTTNG_ERR_INVALID;
1737 goto end;
1738 }
1739 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1740 session->kernel_session, process_attr, value);
1741 break;
1742 case LTTNG_DOMAIN_UST:
1743 if (!session->ust_session) {
1744 ret_code = LTTNG_ERR_INVALID;
1745 goto end;
1746 }
1747 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1748 session->ust_session, process_attr, value);
1749 break;
1750 default:
1751 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1752 break;
1753 }
1754end:
1755 return ret_code;
1756}
1757
1758enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1759 struct ltt_session *session,
1760 enum lttng_domain_type domain,
1761 enum lttng_process_attr process_attr,
1762 struct lttng_process_attr_values **values)
1763{
1764 enum lttng_error_code ret_code = LTTNG_OK;
1765 const struct process_attr_tracker *tracker;
1766 enum process_attr_tracker_status status;
1767
1768 switch (domain) {
1769 case LTTNG_DOMAIN_KERNEL:
1770 if (!session->kernel_session) {
1771 ret_code = LTTNG_ERR_INVALID;
1772 goto end;
1773 }
1774 tracker = kernel_get_process_attr_tracker(
1775 session->kernel_session, process_attr);
1776 break;
1777 case LTTNG_DOMAIN_UST:
1778 if (!session->ust_session) {
1779 ret_code = LTTNG_ERR_INVALID;
1780 goto end;
1781 }
1782 tracker = trace_ust_get_process_attr_tracker(
1783 session->ust_session, process_attr);
1784 break;
1785 default:
1786 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1787 goto end;
1788 }
1789
1790 if (!tracker) {
1791 ret_code = LTTNG_ERR_INVALID;
1792 goto end;
1793 }
1794
1795 status = process_attr_tracker_get_inclusion_set(tracker, values);
1796 switch (status) {
1797 case PROCESS_ATTR_TRACKER_STATUS_OK:
1798 ret_code = LTTNG_OK;
1799 break;
1800 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1801 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1802 break;
1803 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1804 ret_code = LTTNG_ERR_NOMEM;
1805 break;
1806 default:
1807 ret_code = LTTNG_ERR_UNK;
1808 break;
1809 }
1810
1811end:
1812 return ret_code;
1813}
1814
2f77fc4b
DG
1815/*
1816 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1817 */
56a37563 1818int cmd_disable_event(struct ltt_session *session,
df4f5a87
JG
1819 enum lttng_domain_type domain, const char *channel_name,
1820 const struct lttng_event *event)
2f77fc4b
DG
1821{
1822 int ret;
df4f5a87 1823 const char *event_name;
6e911cad 1824
18a720cd
MD
1825 DBG("Disable event command for event \'%s\'", event->name);
1826
6e911cad
MD
1827 event_name = event->name;
1828
9b7431cf
JG
1829 /* Error out on unhandled search criteria */
1830 if (event->loglevel_type || event->loglevel != -1 || event->enabled
6e911cad 1831 || event->pid || event->filter || event->exclusion) {
7076b56e
JG
1832 ret = LTTNG_ERR_UNK;
1833 goto error;
6e911cad 1834 }
2f77fc4b 1835
2223c96f
DG
1836 rcu_read_lock();
1837
2f77fc4b
DG
1838 switch (domain) {
1839 case LTTNG_DOMAIN_KERNEL:
1840 {
1841 struct ltt_kernel_channel *kchan;
1842 struct ltt_kernel_session *ksess;
1843
1844 ksess = session->kernel_session;
1845
85076754
MD
1846 /*
1847 * If a non-default channel has been created in the
1848 * session, explicitely require that -c chan_name needs
1849 * to be provided.
1850 */
1851 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1852 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1853 goto error_unlock;
85076754
MD
1854 }
1855
2f77fc4b
DG
1856 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1857 if (kchan == NULL) {
f73fabfd 1858 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
7076b56e 1859 goto error_unlock;
2f77fc4b
DG
1860 }
1861
6e911cad
MD
1862 switch (event->type) {
1863 case LTTNG_EVENT_ALL:
9550ee81 1864 case LTTNG_EVENT_TRACEPOINT:
d0ae4ea8 1865 case LTTNG_EVENT_SYSCALL:
9550ee81
JR
1866 case LTTNG_EVENT_PROBE:
1867 case LTTNG_EVENT_FUNCTION:
1868 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1869 if (event_name[0] == '\0') {
1870 ret = event_kernel_disable_event(kchan,
1871 NULL, event->type);
29c62722 1872 } else {
d0ae4ea8 1873 ret = event_kernel_disable_event(kchan,
9550ee81 1874 event_name, event->type);
29c62722 1875 }
6e911cad 1876 if (ret != LTTNG_OK) {
7076b56e 1877 goto error_unlock;
6e911cad
MD
1878 }
1879 break;
6e911cad
MD
1880 default:
1881 ret = LTTNG_ERR_UNK;
7076b56e 1882 goto error_unlock;
2f77fc4b
DG
1883 }
1884
7d268848 1885 kernel_wait_quiescent();
2f77fc4b
DG
1886 break;
1887 }
1888 case LTTNG_DOMAIN_UST:
1889 {
1890 struct ltt_ust_channel *uchan;
1891 struct ltt_ust_session *usess;
1892
1893 usess = session->ust_session;
1894
7076b56e
JG
1895 if (validate_ust_event_name(event_name)) {
1896 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1897 goto error_unlock;
1898 }
1899
85076754
MD
1900 /*
1901 * If a non-default channel has been created in the
9550ee81 1902 * session, explicitly require that -c chan_name needs
85076754
MD
1903 * to be provided.
1904 */
1905 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1906 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1907 goto error_unlock;
85076754
MD
1908 }
1909
2f77fc4b
DG
1910 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1911 channel_name);
1912 if (uchan == NULL) {
f73fabfd 1913 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
7076b56e 1914 goto error_unlock;
2f77fc4b
DG
1915 }
1916
6e911cad
MD
1917 switch (event->type) {
1918 case LTTNG_EVENT_ALL:
b3639870
JR
1919 /*
1920 * An empty event name means that everything
1921 * should be disabled.
1922 */
1923 if (event->name[0] == '\0') {
1924 ret = event_ust_disable_all_tracepoints(usess, uchan);
77d536b2
JR
1925 } else {
1926 ret = event_ust_disable_tracepoint(usess, uchan,
1927 event_name);
1928 }
6e911cad 1929 if (ret != LTTNG_OK) {
7076b56e 1930 goto error_unlock;
6e911cad
MD
1931 }
1932 break;
1933 default:
1934 ret = LTTNG_ERR_UNK;
7076b56e 1935 goto error_unlock;
2f77fc4b
DG
1936 }
1937
1938 DBG3("Disable UST event %s in channel %s completed", event_name,
1939 channel_name);
1940 break;
1941 }
5cdb6027 1942 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1943 case LTTNG_DOMAIN_JUL:
0e115563 1944 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1945 {
fefd409b 1946 struct agent *agt;
f20baf8e
DG
1947 struct ltt_ust_session *usess = session->ust_session;
1948
1949 assert(usess);
1950
6e911cad
MD
1951 switch (event->type) {
1952 case LTTNG_EVENT_ALL:
1953 break;
1954 default:
1955 ret = LTTNG_ERR_UNK;
7076b56e 1956 goto error_unlock;
6e911cad
MD
1957 }
1958
5cdb6027 1959 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1960 if (!agt) {
1961 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
7076b56e 1962 goto error_unlock;
fefd409b 1963 }
b3639870
JR
1964 /*
1965 * An empty event name means that everything
1966 * should be disabled.
1967 */
1968 if (event->name[0] == '\0') {
18a720cd
MD
1969 ret = event_agent_disable_all(usess, agt);
1970 } else {
1971 ret = event_agent_disable(usess, agt, event_name);
1972 }
f20baf8e 1973 if (ret != LTTNG_OK) {
7076b56e 1974 goto error_unlock;
f20baf8e
DG
1975 }
1976
1977 break;
1978 }
2f77fc4b 1979 default:
f73fabfd 1980 ret = LTTNG_ERR_UND;
7076b56e 1981 goto error_unlock;
2f77fc4b
DG
1982 }
1983
f73fabfd 1984 ret = LTTNG_OK;
2f77fc4b 1985
7076b56e 1986error_unlock:
2223c96f 1987 rcu_read_unlock();
7076b56e 1988error:
2f77fc4b
DG
1989 return ret;
1990}
1991
2f77fc4b
DG
1992/*
1993 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1994 */
56a37563 1995int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
df4f5a87 1996 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1997{
d5979e4a 1998 int ret, chan_kern_created = 0, chan_ust_created = 0;
bdf64013
JG
1999 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
2000
9a699f7b
JR
2001 /*
2002 * Don't try to add a context if the session has been started at
2003 * some point in time before. The tracer does not allow it and would
2004 * result in a corrupted trace.
2005 */
2006 if (session->has_been_started) {
2007 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2008 goto end;
2009 }
2010
bdf64013
JG
2011 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2012 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
2013 app_ctx_name = ctx->u.app_ctx.ctx_name;
2014 }
2f77fc4b
DG
2015
2016 switch (domain) {
2017 case LTTNG_DOMAIN_KERNEL:
979e618e
DG
2018 assert(session->kernel_session);
2019
2020 if (session->kernel_session->channel_count == 0) {
2021 /* Create default channel */
2022 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
2023 if (ret != LTTNG_OK) {
2024 goto error;
2025 }
d5979e4a 2026 chan_kern_created = 1;
979e618e 2027 }
2f77fc4b 2028 /* Add kernel context to kernel tracer */
601d5acf 2029 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 2030 if (ret != LTTNG_OK) {
2f77fc4b
DG
2031 goto error;
2032 }
2033 break;
bdf64013
JG
2034 case LTTNG_DOMAIN_JUL:
2035 case LTTNG_DOMAIN_LOG4J:
2036 {
2037 /*
2038 * Validate channel name.
2039 * If no channel name is given and the domain is JUL or LOG4J,
2040 * set it to the appropriate domain-specific channel name. If
2041 * a name is provided but does not match the expexted channel
2042 * name, return an error.
2043 */
2044 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
2045 strcmp(channel_name,
2046 DEFAULT_JUL_CHANNEL_NAME)) {
2047 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2048 goto error;
2049 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
2050 strcmp(channel_name,
2051 DEFAULT_LOG4J_CHANNEL_NAME)) {
2052 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2053 goto error;
2054 }
a93b3916 2055 /* break is _not_ missing here. */
bdf64013 2056 }
2f77fc4b
DG
2057 case LTTNG_DOMAIN_UST:
2058 {
2059 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
2060 unsigned int chan_count;
2061
2f77fc4b
DG
2062 assert(usess);
2063
85076754 2064 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
2065 if (chan_count == 0) {
2066 struct lttng_channel *attr;
2067 /* Create default channel */
0a9c6494 2068 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
2069 if (attr == NULL) {
2070 ret = LTTNG_ERR_FATAL;
2071 goto error;
2072 }
2073
7972aab2 2074 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
2075 if (ret != LTTNG_OK) {
2076 free(attr);
2077 goto error;
2078 }
cf0bcb51 2079 channel_attr_destroy(attr);
d5979e4a 2080 chan_ust_created = 1;
979e618e
DG
2081 }
2082
601d5acf 2083 ret = context_ust_add(usess, domain, ctx, channel_name);
bdf64013
JG
2084 free(app_ctx_provider_name);
2085 free(app_ctx_name);
2086 app_ctx_name = NULL;
2087 app_ctx_provider_name = NULL;
f73fabfd 2088 if (ret != LTTNG_OK) {
2f77fc4b
DG
2089 goto error;
2090 }
2091 break;
2092 }
2f77fc4b 2093 default:
f73fabfd 2094 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2095 goto error;
2096 }
2097
bdf64013
JG
2098 ret = LTTNG_OK;
2099 goto end;
2f77fc4b
DG
2100
2101error:
d5979e4a
DG
2102 if (chan_kern_created) {
2103 struct ltt_kernel_channel *kchan =
2104 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2105 session->kernel_session);
2106 /* Created previously, this should NOT fail. */
2107 assert(kchan);
2108 kernel_destroy_channel(kchan);
2109 }
2110
2111 if (chan_ust_created) {
2112 struct ltt_ust_channel *uchan =
2113 trace_ust_find_channel_by_name(
2114 session->ust_session->domain_global.channels,
2115 DEFAULT_CHANNEL_NAME);
2116 /* Created previously, this should NOT fail. */
2117 assert(uchan);
2118 /* Remove from the channel list of the session. */
2119 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2120 uchan);
2121 trace_ust_destroy_channel(uchan);
2122 }
bdf64013
JG
2123end:
2124 free(app_ctx_provider_name);
2125 free(app_ctx_name);
2f77fc4b
DG
2126 return ret;
2127}
2128
dac8e046
JG
2129static inline bool name_starts_with(const char *name, const char *prefix)
2130{
2131 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2132
2133 return !strncmp(name, prefix, max_cmp_len);
2134}
2135
2136/* Perform userspace-specific event name validation */
2137static int validate_ust_event_name(const char *name)
2138{
2139 int ret = 0;
2140
2141 if (!name) {
2142 ret = -1;
2143 goto end;
2144 }
2145
2146 /*
2147 * Check name against all internal UST event component namespaces used
2148 * by the agents.
2149 */
2150 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2151 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2152 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2153 ret = -1;
2154 }
2155
2156end:
2157 return ret;
2158}
88f06f15 2159
2f77fc4b 2160/*
88f06f15
JG
2161 * Internal version of cmd_enable_event() with a supplemental
2162 * "internal_event" flag which is used to enable internal events which should
2163 * be hidden from clients. Such events are used in the agent implementation to
2164 * enable the events through which all "agent" events are funeled.
2f77fc4b 2165 */
88f06f15 2166static int _cmd_enable_event(struct ltt_session *session,
df4f5a87 2167 const struct lttng_domain *domain,
025faf73 2168 char *channel_name, struct lttng_event *event,
6b453b5e 2169 char *filter_expression,
db8f1377
JI
2170 struct lttng_filter_bytecode *filter,
2171 struct lttng_event_exclusion *exclusion,
88f06f15 2172 int wpipe, bool internal_event)
2f77fc4b 2173{
9f449915 2174 int ret = 0, channel_created = 0;
cfedea03 2175 struct lttng_channel *attr = NULL;
2f77fc4b
DG
2176
2177 assert(session);
2178 assert(event);
2179 assert(channel_name);
2180
2a385866
JG
2181 /* If we have a filter, we must have its filter expression */
2182 assert(!(!!filter_expression ^ !!filter));
2183
9f449915
PP
2184 /* Normalize event name as a globbing pattern */
2185 strutils_normalize_star_glob_pattern(event->name);
18a720cd 2186
9f449915
PP
2187 /* Normalize exclusion names as globbing patterns */
2188 if (exclusion) {
2189 size_t i;
f5ac4bd7 2190
9f449915
PP
2191 for (i = 0; i < exclusion->count; i++) {
2192 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2193
2194 strutils_normalize_star_glob_pattern(name);
2195 }
930a2e99
JG
2196 }
2197
9f449915
PP
2198 DBG("Enable event command for event \'%s\'", event->name);
2199
2200 rcu_read_lock();
2201
7972aab2 2202 switch (domain->type) {
2f77fc4b
DG
2203 case LTTNG_DOMAIN_KERNEL:
2204 {
2205 struct ltt_kernel_channel *kchan;
2206
85076754
MD
2207 /*
2208 * If a non-default channel has been created in the
2209 * session, explicitely require that -c chan_name needs
2210 * to be provided.
2211 */
2212 if (session->kernel_session->has_non_default_channel
2213 && channel_name[0] == '\0') {
2214 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2215 goto error;
2216 }
2217
2f77fc4b
DG
2218 kchan = trace_kernel_get_channel_by_name(channel_name,
2219 session->kernel_session);
2220 if (kchan == NULL) {
0a9c6494
DG
2221 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2222 LTTNG_BUFFER_GLOBAL);
2f77fc4b 2223 if (attr == NULL) {
f73fabfd 2224 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2225 goto error;
2226 }
04c17253
MD
2227 if (lttng_strncpy(attr->name, channel_name,
2228 sizeof(attr->name))) {
2229 ret = LTTNG_ERR_INVALID;
04c17253
MD
2230 goto error;
2231 }
2f77fc4b
DG
2232
2233 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 2234 if (ret != LTTNG_OK) {
2f77fc4b
DG
2235 goto error;
2236 }
e5f5db7f 2237 channel_created = 1;
2f77fc4b
DG
2238 }
2239
2240 /* Get the newly created kernel channel pointer */
2241 kchan = trace_kernel_get_channel_by_name(channel_name,
2242 session->kernel_session);
2243 if (kchan == NULL) {
2244 /* This sould not happen... */
f73fabfd 2245 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2246 goto error;
2247 }
2248
6e911cad
MD
2249 switch (event->type) {
2250 case LTTNG_EVENT_ALL:
29c62722 2251 {
00a62084
MD
2252 char *filter_expression_a = NULL;
2253 struct lttng_filter_bytecode *filter_a = NULL;
2254
2255 /*
2256 * We need to duplicate filter_expression and filter,
2257 * because ownership is passed to first enable
2258 * event.
2259 */
2260 if (filter_expression) {
2261 filter_expression_a = strdup(filter_expression);
2262 if (!filter_expression_a) {
2263 ret = LTTNG_ERR_FATAL;
2264 goto error;
2265 }
2266 }
2267 if (filter) {
2268 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2269 if (!filter_a) {
2270 free(filter_expression_a);
2271 ret = LTTNG_ERR_FATAL;
2272 goto error;
2273 }
2274 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2275 }
29c62722 2276 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
00a62084
MD
2277 ret = event_kernel_enable_event(kchan, event,
2278 filter_expression, filter);
a969e101
MD
2279 /* We have passed ownership */
2280 filter_expression = NULL;
2281 filter = NULL;
29c62722
MD
2282 if (ret != LTTNG_OK) {
2283 if (channel_created) {
2284 /* Let's not leak a useless channel. */
2285 kernel_destroy_channel(kchan);
2286 }
00a62084
MD
2287 free(filter_expression_a);
2288 free(filter_a);
29c62722
MD
2289 goto error;
2290 }
2291 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
00a62084
MD
2292 ret = event_kernel_enable_event(kchan, event,
2293 filter_expression_a, filter_a);
60d21fa2
AB
2294 /* We have passed ownership */
2295 filter_expression_a = NULL;
2296 filter_a = NULL;
29c62722
MD
2297 if (ret != LTTNG_OK) {
2298 goto error;
2299 }
2300 break;
2301 }
6e6ef3d7 2302 case LTTNG_EVENT_PROBE:
dcabc190 2303 case LTTNG_EVENT_USERSPACE_PROBE:
6e6ef3d7
DG
2304 case LTTNG_EVENT_FUNCTION:
2305 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 2306 case LTTNG_EVENT_TRACEPOINT:
00a62084
MD
2307 ret = event_kernel_enable_event(kchan, event,
2308 filter_expression, filter);
a969e101
MD
2309 /* We have passed ownership */
2310 filter_expression = NULL;
2311 filter = NULL;
6e911cad
MD
2312 if (ret != LTTNG_OK) {
2313 if (channel_created) {
2314 /* Let's not leak a useless channel. */
2315 kernel_destroy_channel(kchan);
2316 }
2317 goto error;
e5f5db7f 2318 }
6e911cad
MD
2319 break;
2320 case LTTNG_EVENT_SYSCALL:
00a62084
MD
2321 ret = event_kernel_enable_event(kchan, event,
2322 filter_expression, filter);
a969e101
MD
2323 /* We have passed ownership */
2324 filter_expression = NULL;
2325 filter = NULL;
e2b957af
MD
2326 if (ret != LTTNG_OK) {
2327 goto error;
2328 }
6e911cad
MD
2329 break;
2330 default:
2331 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
2332 goto error;
2333 }
2334
7d268848 2335 kernel_wait_quiescent();
2f77fc4b
DG
2336 break;
2337 }
2338 case LTTNG_DOMAIN_UST:
2339 {
2340 struct ltt_ust_channel *uchan;
2341 struct ltt_ust_session *usess = session->ust_session;
2342
2343 assert(usess);
2344
85076754
MD
2345 /*
2346 * If a non-default channel has been created in the
2347 * session, explicitely require that -c chan_name needs
2348 * to be provided.
2349 */
2350 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2351 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2352 goto error;
2353 }
2354
2f77fc4b
DG
2355 /* Get channel from global UST domain */
2356 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2357 channel_name);
2358 if (uchan == NULL) {
2359 /* Create default channel */
0a9c6494
DG
2360 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2361 usess->buffer_type);
2f77fc4b 2362 if (attr == NULL) {
f73fabfd 2363 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2364 goto error;
2365 }
04c17253
MD
2366 if (lttng_strncpy(attr->name, channel_name,
2367 sizeof(attr->name))) {
2368 ret = LTTNG_ERR_INVALID;
04c17253
MD
2369 goto error;
2370 }
2f77fc4b
DG
2371
2372 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 2373 if (ret != LTTNG_OK) {
2f77fc4b
DG
2374 goto error;
2375 }
2f77fc4b
DG
2376
2377 /* Get the newly created channel reference back */
2378 uchan = trace_ust_find_channel_by_name(
2379 usess->domain_global.channels, channel_name);
2380 assert(uchan);
2381 }
2382
141feb8c
JG
2383 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2384 /*
2385 * Don't allow users to add UST events to channels which
2386 * are assigned to a userspace subdomain (JUL, Log4J,
2387 * Python, etc.).
2388 */
2389 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2390 goto error;
2391 }
2392
dac8e046
JG
2393 if (!internal_event) {
2394 /*
2395 * Ensure the event name is not reserved for internal
2396 * use.
2397 */
2398 ret = validate_ust_event_name(event->name);
2399 if (ret) {
2400 WARN("Userspace event name %s failed validation.",
bbcab087 2401 event->name);
dac8e046
JG
2402 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2403 goto error;
2404 }
2405 }
2406
2f77fc4b 2407 /* At this point, the session and channel exist on the tracer */
6b453b5e 2408 ret = event_ust_enable_tracepoint(usess, uchan, event,
88f06f15
JG
2409 filter_expression, filter, exclusion,
2410 internal_event);
49d21f93
MD
2411 /* We have passed ownership */
2412 filter_expression = NULL;
2413 filter = NULL;
2414 exclusion = NULL;
94382e15
JG
2415 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2416 goto already_enabled;
2417 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2418 goto error;
2419 }
2420 break;
2421 }
5cdb6027 2422 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2423 case LTTNG_DOMAIN_JUL:
0e115563 2424 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2425 {
da6c3a50 2426 const char *default_event_name, *default_chan_name;
fefd409b 2427 struct agent *agt;
f20baf8e
DG
2428 struct lttng_event uevent;
2429 struct lttng_domain tmp_dom;
2430 struct ltt_ust_session *usess = session->ust_session;
2431
2432 assert(usess);
2433
f28f9e44
JG
2434 if (!agent_tracing_is_enabled()) {
2435 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2436 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2437 goto error;
2438 }
2439
5cdb6027 2440 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2441 if (!agt) {
5cdb6027 2442 agt = agent_create(domain->type);
fefd409b 2443 if (!agt) {
e5b3c48c 2444 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2445 goto error;
2446 }
2447 agent_add(agt, usess->agents);
2448 }
2449
022d91ba 2450 /* Create the default tracepoint. */
996de3c7 2451 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2452 uevent.type = LTTNG_EVENT_TRACEPOINT;
2453 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
51755dc8
JG
2454 default_event_name = event_get_default_agent_ust_name(
2455 domain->type);
da6c3a50 2456 if (!default_event_name) {
e5b3c48c 2457 ret = LTTNG_ERR_FATAL;
da6c3a50 2458 goto error;
f43f95a9 2459 }
da6c3a50 2460 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2461 uevent.name[sizeof(uevent.name) - 1] = '\0';
2462
2463 /*
2464 * The domain type is changed because we are about to enable the
2465 * default channel and event for the JUL domain that are hardcoded.
2466 * This happens in the UST domain.
2467 */
2468 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2469 tmp_dom.type = LTTNG_DOMAIN_UST;
2470
0e115563
DG
2471 switch (domain->type) {
2472 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2473 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2474 break;
2475 case LTTNG_DOMAIN_JUL:
da6c3a50 2476 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2477 break;
2478 case LTTNG_DOMAIN_PYTHON:
2479 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2480 break;
2481 default:
e98a44b0 2482 /* The switch/case we are in makes this impossible */
0e115563 2483 assert(0);
da6c3a50
DG
2484 }
2485
971da06a 2486 {
8404118c 2487 char *filter_expression_copy = NULL;
51755dc8 2488 struct lttng_filter_bytecode *filter_copy = NULL;
971da06a
JG
2489
2490 if (filter) {
51755dc8
JG
2491 const size_t filter_size = sizeof(
2492 struct lttng_filter_bytecode)
2493 + filter->len;
2494
2495 filter_copy = zmalloc(filter_size);
971da06a 2496 if (!filter_copy) {
018096a4 2497 ret = LTTNG_ERR_NOMEM;
b742e3e2 2498 goto error;
971da06a 2499 }
51755dc8 2500 memcpy(filter_copy, filter, filter_size);
971da06a 2501
8404118c
JG
2502 filter_expression_copy =
2503 strdup(filter_expression);
2504 if (!filter_expression) {
2505 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2506 }
2507
2508 if (!filter_expression_copy || !filter_copy) {
2509 free(filter_expression_copy);
2510 free(filter_copy);
2511 goto error;
8404118c 2512 }
971da06a
JG
2513 }
2514
88f06f15 2515 ret = cmd_enable_event_internal(session, &tmp_dom,
971da06a 2516 (char *) default_chan_name,
8404118c
JG
2517 &uevent, filter_expression_copy,
2518 filter_copy, NULL, wpipe);
971da06a
JG
2519 }
2520
94382e15
JG
2521 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2522 goto already_enabled;
2523 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2524 goto error;
2525 }
2526
2527 /* The wild card * means that everything should be enabled. */
2528 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
8404118c
JG
2529 ret = event_agent_enable_all(usess, agt, event, filter,
2530 filter_expression);
f20baf8e 2531 } else {
8404118c
JG
2532 ret = event_agent_enable(usess, agt, event, filter,
2533 filter_expression);
f20baf8e 2534 }
51755dc8
JG
2535 filter = NULL;
2536 filter_expression = NULL;
f20baf8e
DG
2537 if (ret != LTTNG_OK) {
2538 goto error;
2539 }
2540
2541 break;
2542 }
2f77fc4b 2543 default:
f73fabfd 2544 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2545 goto error;
2546 }
2547
f73fabfd 2548 ret = LTTNG_OK;
2f77fc4b 2549
94382e15 2550already_enabled:
2f77fc4b 2551error:
49d21f93
MD
2552 free(filter_expression);
2553 free(filter);
2554 free(exclusion);
cf0bcb51 2555 channel_attr_destroy(attr);
2223c96f 2556 rcu_read_unlock();
2f77fc4b
DG
2557 return ret;
2558}
2559
88f06f15
JG
2560/*
2561 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2562 * We own filter, exclusion, and filter_expression.
2563 */
df4f5a87
JG
2564int cmd_enable_event(struct ltt_session *session,
2565 const struct lttng_domain *domain,
88f06f15
JG
2566 char *channel_name, struct lttng_event *event,
2567 char *filter_expression,
2568 struct lttng_filter_bytecode *filter,
2569 struct lttng_event_exclusion *exclusion,
2570 int wpipe)
2571{
2572 return _cmd_enable_event(session, domain, channel_name, event,
2573 filter_expression, filter, exclusion, wpipe, false);
2574}
2575
2576/*
2577 * Enable an event which is internal to LTTng. An internal should
2578 * never be made visible to clients and are immune to checks such as
2579 * reserved names.
2580 */
2581static int cmd_enable_event_internal(struct ltt_session *session,
df4f5a87 2582 const struct lttng_domain *domain,
88f06f15
JG
2583 char *channel_name, struct lttng_event *event,
2584 char *filter_expression,
2585 struct lttng_filter_bytecode *filter,
2586 struct lttng_event_exclusion *exclusion,
2587 int wpipe)
2588{
2589 return _cmd_enable_event(session, domain, channel_name, event,
2590 filter_expression, filter, exclusion, wpipe, true);
2591}
2592
2f77fc4b
DG
2593/*
2594 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2595 */
56a37563
JG
2596ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2597 struct lttng_event **events)
2f77fc4b
DG
2598{
2599 int ret;
2600 ssize_t nb_events = 0;
2601
2602 switch (domain) {
2603 case LTTNG_DOMAIN_KERNEL:
7d268848 2604 nb_events = kernel_list_events(events);
2f77fc4b 2605 if (nb_events < 0) {
f73fabfd 2606 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
2607 goto error;
2608 }
2609 break;
2610 case LTTNG_DOMAIN_UST:
2611 nb_events = ust_app_list_events(events);
2612 if (nb_events < 0) {
f73fabfd 2613 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2614 goto error;
2615 }
2616 break;
5cdb6027 2617 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2618 case LTTNG_DOMAIN_JUL:
0e115563 2619 case LTTNG_DOMAIN_PYTHON:
f60140a1 2620 nb_events = agent_list_events(events, domain);
3c6a091f
DG
2621 if (nb_events < 0) {
2622 ret = LTTNG_ERR_UST_LIST_FAIL;
2623 goto error;
2624 }
2625 break;
2f77fc4b 2626 default:
f73fabfd 2627 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2628 goto error;
2629 }
2630
2631 return nb_events;
2632
2633error:
2634 /* Return negative value to differentiate return code */
2635 return -ret;
2636}
2637
2638/*
2639 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2640 */
56a37563 2641ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2f77fc4b
DG
2642 struct lttng_event_field **fields)
2643{
2644 int ret;
2645 ssize_t nb_fields = 0;
2646
2647 switch (domain) {
2648 case LTTNG_DOMAIN_UST:
2649 nb_fields = ust_app_list_event_fields(fields);
2650 if (nb_fields < 0) {
f73fabfd 2651 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2652 goto error;
2653 }
2654 break;
2655 case LTTNG_DOMAIN_KERNEL:
2656 default: /* fall-through */
f73fabfd 2657 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2658 goto error;
2659 }
2660
2661 return nb_fields;
2662
2663error:
2664 /* Return negative value to differentiate return code */
2665 return -ret;
2666}
2667
834978fd
DG
2668ssize_t cmd_list_syscalls(struct lttng_event **events)
2669{
2670 return syscall_table_list(events);
2671}
2672
2f77fc4b
DG
2673/*
2674 * Command LTTNG_START_TRACE processed by the client thread.
a9ad0c8f
MD
2675 *
2676 * Called with session mutex held.
2f77fc4b
DG
2677 */
2678int cmd_start_trace(struct ltt_session *session)
2679{
82b69413 2680 enum lttng_error_code ret;
cde3e505 2681 unsigned long nb_chan = 0;
2f77fc4b
DG
2682 struct ltt_kernel_session *ksession;
2683 struct ltt_ust_session *usess;
1f496244
JG
2684 const bool session_rotated_after_last_stop =
2685 session->rotated_after_last_stop;
b02f5986
MD
2686 const bool session_cleared_after_last_stop =
2687 session->cleared_after_last_stop;
2f77fc4b
DG
2688
2689 assert(session);
2690
2691 /* Ease our life a bit ;) */
2692 ksession = session->kernel_session;
2693 usess = session->ust_session;
2694
8382cf6f
DG
2695 /* Is the session already started? */
2696 if (session->active) {
f73fabfd 2697 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
7a24ece3
JR
2698 /* Perform nothing */
2699 goto end;
2f77fc4b
DG
2700 }
2701
1f496244
JG
2702 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2703 !session->current_trace_chunk) {
2704 /*
2705 * A rotation was launched while the session was stopped and
2706 * it has not been completed yet. It is not possible to start
2707 * the session since starting the session here would require a
2708 * rotation from "NULL" to a new trace chunk. That rotation
2709 * would overlap with the ongoing rotation, which is not
2710 * supported.
2711 */
2712 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2713 session->name);
2714 ret = LTTNG_ERR_ROTATION_PENDING;
2715 goto error;
2716 }
2717
cde3e505
DG
2718 /*
2719 * Starting a session without channel is useless since after that it's not
2720 * possible to enable channel thus inform the client.
2721 */
2722 if (usess && usess->domain_global.channels) {
2723 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2724 }
2725 if (ksession) {
2726 nb_chan += ksession->channel_count;
2727 }
2728 if (!nb_chan) {
2729 ret = LTTNG_ERR_NO_CHANNEL;
2730 goto error;
2731 }
2732
1f496244
JG
2733 session->active = 1;
2734 session->rotated_after_last_stop = false;
b02f5986 2735 session->cleared_after_last_stop = false;
070b6a86 2736 if (session->output_traces && !session->current_trace_chunk) {
1f496244
JG
2737 if (!session->has_been_started) {
2738 struct lttng_trace_chunk *trace_chunk;
2739
2740 DBG("Creating initial trace chunk of session \"%s\"",
2741 session->name);
2742 trace_chunk = session_create_new_trace_chunk(
2743 session, NULL, NULL, NULL);
2744 if (!trace_chunk) {
2745 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2746 goto error;
2747 }
2748 assert(!session->current_trace_chunk);
2749 ret = session_set_trace_chunk(session, trace_chunk,
2750 NULL);
2751 lttng_trace_chunk_put(trace_chunk);
2752 if (ret) {
2753 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2754 goto error;
2755 }
2756 } else {
2757 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2758 session->name);
2759 /*
2760 * Rotate existing streams into the new chunk.
2761 * This is a "quiet" rotation has no client has
2762 * explicitly requested this operation.
2763 *
2764 * There is also no need to wait for the rotation
2765 * to complete as it will happen immediately. No data
2766 * was produced as the session was stopped, so the
2767 * rotation should happen on reception of the command.
2768 */
343defc2
MD
2769 ret = cmd_rotate_session(session, NULL, true,
2770 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
1f496244
JG
2771 if (ret != LTTNG_OK) {
2772 goto error;
2773 }
5c408ad8 2774 }
c996624c
JD
2775 }
2776
2f77fc4b
DG
2777 /* Kernel tracing */
2778 if (ksession != NULL) {
c996624c 2779 DBG("Start kernel tracing session %s", session->name);
7d268848 2780 ret = start_kernel_session(ksession);
9b6c7ec5 2781 if (ret != LTTNG_OK) {
2f77fc4b
DG
2782 goto error;
2783 }
2f77fc4b
DG
2784 }
2785
2786 /* Flag session that trace should start automatically */
2787 if (usess) {
82b69413
JG
2788 int int_ret = ust_app_start_trace_all(usess);
2789
2790 if (int_ret < 0) {
f73fabfd 2791 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
2792 goto error;
2793 }
2794 }
2795
04ed9e10
JG
2796 /*
2797 * Open a packet in every stream of the session to ensure that viewers
2798 * can correctly identify the boundaries of the periods during which
2799 * tracing was active for this session.
2800 */
2801 ret = session_open_packets(session);
2802 if (ret != LTTNG_OK) {
2803 goto error;
2804 }
2805
5c408ad8
JD
2806 /*
2807 * Clear the flag that indicates that a rotation was done while the
2808 * session was stopped.
2809 */
2810 session->rotated_after_last_stop = false;
2811
259c2674 2812 if (session->rotate_timer_period) {
82b69413
JG
2813 int int_ret = timer_session_rotation_schedule_timer_start(
2814 session, session->rotate_timer_period);
2815
2816 if (int_ret < 0) {
259c2674
JD
2817 ERR("Failed to enable rotate timer");
2818 ret = LTTNG_ERR_UNK;
2819 goto error;
2820 }
2821 }
2822
f73fabfd 2823 ret = LTTNG_OK;
2f77fc4b
DG
2824
2825error:
1f496244
JG
2826 if (ret == LTTNG_OK) {
2827 /* Flag this after a successful start. */
2828 session->has_been_started |= 1;
2829 } else {
2830 session->active = 0;
2831 /* Restore initial state on error. */
2832 session->rotated_after_last_stop =
2833 session_rotated_after_last_stop;
b02f5986
MD
2834 session->cleared_after_last_stop =
2835 session_cleared_after_last_stop;
1f496244 2836 }
7a24ece3 2837end:
2f77fc4b
DG
2838 return ret;
2839}
2840
2841/*
2842 * Command LTTNG_STOP_TRACE processed by the client thread.
2843 */
2844int cmd_stop_trace(struct ltt_session *session)
2845{
2846 int ret;
2f77fc4b
DG
2847 struct ltt_kernel_session *ksession;
2848 struct ltt_ust_session *usess;
2849
2850 assert(session);
2851
4dbe1875 2852 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2f77fc4b
DG
2853 /* Short cut */
2854 ksession = session->kernel_session;
2855 usess = session->ust_session;
2856
8382cf6f
DG
2857 /* Session is not active. Skip everythong and inform the client. */
2858 if (!session->active) {
f73fabfd 2859 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
2860 goto error;
2861 }
2862
4dbe1875
MD
2863 ret = stop_kernel_session(ksession);
2864 if (ret != LTTNG_OK) {
2865 goto error;
2f77fc4b
DG
2866 }
2867
14fb1ebe 2868 if (usess && usess->active) {
2f77fc4b
DG
2869 ret = ust_app_stop_trace_all(usess);
2870 if (ret < 0) {
f73fabfd 2871 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2872 goto error;
2873 }
2874 }
2875
4dbe1875
MD
2876 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2877 session->id);
8382cf6f
DG
2878 /* Flag inactive after a successful stop. */
2879 session->active = 0;
4dbe1875 2880 ret = LTTNG_OK;
2f77fc4b
DG
2881
2882error:
2883 return ret;
2884}
2885
2886/*
433f5ba9
JR
2887 * Set the base_path of the session only if subdir of a control uris is set.
2888 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2f77fc4b 2889 */
433f5ba9
JR
2890static int set_session_base_path_from_uris(struct ltt_session *session,
2891 size_t nb_uri,
bda32d56 2892 struct lttng_uri *uris)
2f77fc4b 2893{
433f5ba9
JR
2894 int ret;
2895 size_t i;
2f77fc4b 2896
e3876bf0
JR
2897 for (i = 0; i < nb_uri; i++) {
2898 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2899 uris[i].subdir[0] == '\0') {
2900 /* Not interested in these URIs */
2901 continue;
2902 }
2903
2904 if (session->base_path != NULL) {
2905 free(session->base_path);
2906 session->base_path = NULL;
2907 }
2908
2909 /* Set session base_path */
2910 session->base_path = strdup(uris[i].subdir);
2911 if (!session->base_path) {
433f5ba9
JR
2912 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2913 uris[i].subdir, session->name);
2914 ret = LTTNG_ERR_NOMEM;
e3876bf0
JR
2915 goto error;
2916 }
433f5ba9
JR
2917 DBG2("Setting base path \"%s\" for session \"%s\"",
2918 session->base_path, session->name);
2919 }
2920 ret = LTTNG_OK;
2921error:
2922 return ret;
2923}
2924
2925/*
2926 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2927 */
2928int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2929 struct lttng_uri *uris)
2930{
2931 int ret, i;
2932 struct ltt_kernel_session *ksess = session->kernel_session;
2933 struct ltt_ust_session *usess = session->ust_session;
2934
2935 assert(session);
2936 assert(uris);
2937 assert(nb_uri > 0);
2938
2939 /* Can't set consumer URI if the session is active. */
2940 if (session->active) {
2941 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2942 goto error;
2943 }
2944
2945 /*
2946 * Set the session base path if any. This is done inside
2947 * cmd_set_consumer_uri to preserve backward compatibility of the
2948 * previous session creation api vs the session descriptor api.
2949 */
2950 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2951 if (ret != LTTNG_OK) {
2952 goto error;
e3876bf0
JR
2953 }
2954
bda32d56 2955 /* Set the "global" consumer URIs */
2f77fc4b 2956 for (i = 0; i < nb_uri; i++) {
e3876bf0
JR
2957 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2958 LTTNG_DOMAIN_NONE);
a74934ba 2959 if (ret != LTTNG_OK) {
2f77fc4b
DG
2960 goto error;
2961 }
2f77fc4b
DG
2962 }
2963
bda32d56
JG
2964 /* Set UST session URIs */
2965 if (session->ust_session) {
2966 for (i = 0; i < nb_uri; i++) {
b178f53e 2967 ret = add_uri_to_consumer(session,
bda32d56 2968 session->ust_session->consumer,
b178f53e 2969 &uris[i], LTTNG_DOMAIN_UST);
bda32d56
JG
2970 if (ret != LTTNG_OK) {
2971 goto error;
2972 }
2973 }
2974 }
2975
2976 /* Set kernel session URIs */
2977 if (session->kernel_session) {
2978 for (i = 0; i < nb_uri; i++) {
b178f53e 2979 ret = add_uri_to_consumer(session,
bda32d56 2980 session->kernel_session->consumer,
b178f53e 2981 &uris[i], LTTNG_DOMAIN_KERNEL);
bda32d56
JG
2982 if (ret != LTTNG_OK) {
2983 goto error;
2984 }
2985 }
2986 }
2987
7ab70fe0
DG
2988 /*
2989 * Make sure to set the session in output mode after we set URI since a
2990 * session can be created without URL (thus flagged in no output mode).
2991 */
2992 session->output_traces = 1;
2993 if (ksess) {
2994 ksess->output_traces = 1;
bda32d56
JG
2995 }
2996
2997 if (usess) {
7ab70fe0
DG
2998 usess->output_traces = 1;
2999 }
3000
2f77fc4b 3001 /* All good! */
f73fabfd 3002 ret = LTTNG_OK;
2f77fc4b
DG
3003
3004error:
3005 return ret;
3006}
3007
b178f53e
JG
3008static
3009enum lttng_error_code set_session_output_from_descriptor(
3010 struct ltt_session *session,
3011 const struct lttng_session_descriptor *descriptor)
2f77fc4b
DG
3012{
3013 int ret;
b178f53e
JG
3014 enum lttng_error_code ret_code = LTTNG_OK;
3015 enum lttng_session_descriptor_type session_type =
3016 lttng_session_descriptor_get_type(descriptor);
3017 enum lttng_session_descriptor_output_type output_type =
3018 lttng_session_descriptor_get_output_type(descriptor);
3019 struct lttng_uri uris[2] = {};
3020 size_t uri_count = 0;
3021
3022 switch (output_type) {
3023 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3024 goto end;
3025 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
3026 lttng_session_descriptor_get_local_output_uri(descriptor,
3027 &uris[0]);
3028 uri_count = 1;
3029 break;
3030 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
3031 lttng_session_descriptor_get_network_output_uris(descriptor,
3032 &uris[0], &uris[1]);
3033 uri_count = 2;
3034 break;
3035 default:
3036 ret_code = LTTNG_ERR_INVALID;
e32d7f27 3037 goto end;
2f77fc4b
DG
3038 }
3039
b178f53e
JG
3040 switch (session_type) {
3041 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3042 {
3043 struct snapshot_output *new_output = NULL;
3044
3045 new_output = snapshot_output_alloc();
3046 if (!new_output) {
3047 ret_code = LTTNG_ERR_NOMEM;
3048 goto end;
3049 }
3050
3051 ret = snapshot_output_init_with_uri(session,
3052 DEFAULT_SNAPSHOT_MAX_SIZE,
3053 NULL, uris, uri_count, session->consumer,
3054 new_output, &session->snapshot);
3055 if (ret < 0) {
3056 ret_code = (ret == -ENOMEM) ?
3057 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
3058 snapshot_output_destroy(new_output);
3059 goto end;
3060 }
3061 snapshot_add_output(&session->snapshot, new_output);
3062 break;
3063 }
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3065 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3066 {
3067 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
3068 break;
3069 }
3070 default:
3071 ret_code = LTTNG_ERR_INVALID;
e32d7f27 3072 goto end;
2f77fc4b 3073 }
b178f53e
JG
3074end:
3075 return ret_code;
3076}
3077
3078static
3079enum lttng_error_code cmd_create_session_from_descriptor(
3080 struct lttng_session_descriptor *descriptor,
3081 const lttng_sock_cred *creds,
3082 const char *home_path)
3083{
3084 int ret;
3085 enum lttng_error_code ret_code;
3086 const char *session_name;
3087 struct ltt_session *new_session = NULL;
3088 enum lttng_session_descriptor_status descriptor_status;
2f77fc4b 3089
e32d7f27 3090 session_lock_list();
b178f53e
JG
3091 if (home_path) {
3092 if (*home_path != '/') {
3093 ERR("Home path provided by client is not absolute");
3094 ret_code = LTTNG_ERR_INVALID;
3095 goto end;
3096 }
3097 }
2f77fc4b 3098
b178f53e
JG
3099 descriptor_status = lttng_session_descriptor_get_session_name(
3100 descriptor, &session_name);
3101 switch (descriptor_status) {
3102 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3103 break;
3104 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3105 session_name = NULL;
3106 break;
3107 default:
3108 ret_code = LTTNG_ERR_INVALID;
3109 goto end;
3110 }
e3876bf0 3111
b178f53e 3112 ret_code = session_create(session_name, creds->uid, creds->gid,
e3876bf0 3113 &new_session);
b178f53e 3114 if (ret_code != LTTNG_OK) {
e32d7f27 3115 goto end;
2f77fc4b
DG
3116 }
3117
b178f53e
JG
3118 if (!session_name) {
3119 ret = lttng_session_descriptor_set_session_name(descriptor,
3120 new_session->name);
3121 if (ret) {
3122 ret_code = LTTNG_ERR_SESSION_FAIL;
3123 goto end;
3124 }
3125 }
3126
3127 if (!lttng_session_descriptor_is_output_destination_initialized(
3128 descriptor)) {
3129 /*
3130 * Only include the session's creation time in the output
3131 * destination if the name of the session itself was
3132 * not auto-generated.
3133 */
3134 ret_code = lttng_session_descriptor_set_default_output(
3135 descriptor,
3136 session_name ? &new_session->creation_time : NULL,
3137 home_path);
3138 if (ret_code != LTTNG_OK) {
e32d7f27 3139 goto end;
2bba9e53 3140 }
2bba9e53 3141 } else {
b178f53e
JG
3142 new_session->has_user_specified_directory =
3143 lttng_session_descriptor_has_output_directory(
3144 descriptor);
2f77fc4b
DG
3145 }
3146
b178f53e
JG
3147 switch (lttng_session_descriptor_get_type(descriptor)) {
3148 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3149 new_session->snapshot_mode = 1;
3150 break;
3151 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3152 new_session->live_timer =
3153 lttng_session_descriptor_live_get_timer_interval(
3154 descriptor);
3155 break;
3156 default:
3157 break;
3158 }
2f77fc4b 3159
b178f53e
JG
3160 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3161 if (ret_code != LTTNG_OK) {
3162 goto end;
3163 }
3164 new_session->consumer->enabled = 1;
3165 ret_code = LTTNG_OK;
e32d7f27 3166end:
b178f53e
JG
3167 /* Release reference provided by the session_create function. */
3168 session_put(new_session);
3169 if (ret_code != LTTNG_OK && new_session) {
3170 /* Release the global reference on error. */
3171 session_destroy(new_session);
e32d7f27 3172 }
b178f53e
JG
3173 session_unlock_list();
3174 return ret_code;
2f77fc4b
DG
3175}
3176
b178f53e
JG
3177enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3178 struct lttng_session_descriptor **return_descriptor)
27babd3a
DG
3179{
3180 int ret;
b178f53e
JG
3181 size_t payload_size;
3182 struct lttng_dynamic_buffer payload;
3183 struct lttng_buffer_view home_dir_view;
3184 struct lttng_buffer_view session_descriptor_view;
3185 struct lttng_session_descriptor *session_descriptor = NULL;
3186 enum lttng_error_code ret_code;
3187
3188 lttng_dynamic_buffer_init(&payload);
3189 if (cmd_ctx->lsm->u.create_session.home_dir_size >=
3190 LTTNG_PATH_MAX) {
3191 ret_code = LTTNG_ERR_INVALID;
3192 goto error;
27babd3a 3193 }
b178f53e
JG
3194 if (cmd_ctx->lsm->u.create_session.session_descriptor_size >
3195 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3196 ret_code = LTTNG_ERR_INVALID;
3197 goto error;
27babd3a
DG
3198 }
3199
b178f53e
JG
3200 payload_size = cmd_ctx->lsm->u.create_session.home_dir_size +
3201 cmd_ctx->lsm->u.create_session.session_descriptor_size;
3202 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3203 if (ret) {
3204 ret_code = LTTNG_ERR_NOMEM;
3205 goto error;
27babd3a
DG
3206 }
3207
b178f53e
JG
3208 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3209 if (ret <= 0) {
3210 ERR("Reception of session descriptor failed, aborting.");
3211 ret_code = LTTNG_ERR_SESSION_FAIL;
3212 goto error;
27babd3a
DG
3213 }
3214
b178f53e
JG
3215 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3216 &payload,
3217 0,
3218 cmd_ctx->lsm->u.create_session.home_dir_size);
3219 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3220 &payload,
3221 cmd_ctx->lsm->u.create_session.home_dir_size,
3222 cmd_ctx->lsm->u.create_session.session_descriptor_size);
27babd3a 3223
b178f53e
JG
3224 ret = lttng_session_descriptor_create_from_buffer(
3225 &session_descriptor_view, &session_descriptor);
3226 if (ret < 0) {
3227 ERR("Failed to create session descriptor from payload of \"create session\" command");
3228 ret_code = LTTNG_ERR_INVALID;
3229 goto error;
3230 }
27babd3a 3231
b178f53e
JG
3232 /*
3233 * Sets the descriptor's auto-generated properties (name, output) if
3234 * needed.
3235 */
3236 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3237 &cmd_ctx->creds,
3238 home_dir_view.size ? home_dir_view.data : NULL);
3239 if (ret_code != LTTNG_OK) {
3240 goto error;
e32d7f27 3241 }
b178f53e
JG
3242
3243 ret_code = LTTNG_OK;
3244 *return_descriptor = session_descriptor;
3245 session_descriptor = NULL;
3246error:
3247 lttng_dynamic_buffer_reset(&payload);
3248 lttng_session_descriptor_destroy(session_descriptor);
3249 return ret_code;
27babd3a
DG
3250}
3251
3e3665b8
JG
3252static
3253void cmd_destroy_session_reply(const struct ltt_session *session,
3254 void *_reply_context)
3255{
3256 int ret;
3257 ssize_t comm_ret;
3258 const struct cmd_destroy_session_reply_context *reply_context =
3259 _reply_context;
3260 struct lttng_dynamic_buffer payload;
3261 struct lttcomm_session_destroy_command_header cmd_header;
3262 struct lttng_trace_archive_location *location = NULL;
3263 struct lttcomm_lttng_msg llm = {
3264 .cmd_type = LTTNG_DESTROY_SESSION,
3285a971 3265 .ret_code = reply_context->destruction_status,
3e3665b8
JG
3266 .pid = UINT32_MAX,
3267 .cmd_header_size =
3268 sizeof(struct lttcomm_session_destroy_command_header),
3269 .data_size = 0,
3270 };
3271 size_t payload_size_before_location;
3272
3273 lttng_dynamic_buffer_init(&payload);
3274
3275 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3276 if (ret) {
3277 ERR("Failed to append session destruction message");
3278 goto error;
3279 }
3280
3281 cmd_header.rotation_state =
3282 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3283 session->rotation_state :
3284 LTTNG_ROTATION_STATE_NO_ROTATION);
3285 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3286 sizeof(cmd_header));
3287 if (ret) {
3288 ERR("Failed to append session destruction command header");
3289 goto error;
3290 }
3291
3292 if (!reply_context->implicit_rotation_on_destroy) {
3293 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3294 session->name);
3295 goto send_reply;
3296 }
3297 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3298 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3299 session->name);
3300 goto send_reply;
3301 }
3302
3303 location = session_get_trace_archive_location(session);
3304 if (!location) {
3305 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3306 session->name);