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