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