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