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