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