Fix: sessiond: event name truncation during listing
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
CommitLineData
2f77fc4b 1/*
ab5be9fa
MJ
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
2f77fc4b 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
2f77fc4b 6 *
2f77fc4b
DG
7 */
8
6c1c0768 9#define _LGPL_SOURCE
2f77fc4b 10#include <assert.h>
6dc3064a 11#include <inttypes.h>
2f77fc4b
DG
12#include <urcu/list.h>
13#include <urcu/uatomic.h>
a503e1ef 14#include <sys/stat.h>
d2956687 15#include <stdio.h>
2f77fc4b
DG
16
17#include <common/defaults.h>
18#include <common/common.h>
19#include <common/sessiond-comm/sessiond-comm.h>
20#include <common/relayd/relayd.h>
10a50311 21#include <common/utils.h>
f5436bfc 22#include <common/compat/string.h>
93ec662e 23#include <common/kernel-ctl/kernel-ctl.h>
b0880ae5
JG
24#include <common/dynamic-buffer.h>
25#include <common/buffer-view.h>
9e620ea7
JG
26#include <common/payload.h>
27#include <common/payload-view.h>
82b69413 28#include <common/trace-chunk.h>
3e3665b8 29#include <lttng/location-internal.h>
b0880ae5
JG
30#include <lttng/trigger/trigger-internal.h>
31#include <lttng/condition/condition.h>
32#include <lttng/action/action.h>
6e21424e 33#include <lttng/channel.h>
b0880ae5 34#include <lttng/channel-internal.h>
5c408ad8 35#include <lttng/rotate-internal.h>
17dd1232 36#include <lttng/location-internal.h>
b178f53e 37#include <lttng/session-internal.h>
3c02e545 38#include <lttng/userspace-probe-internal.h>
b178f53e 39#include <lttng/session-descriptor-internal.h>
47aa1ca6
JG
40#include <lttng/lttng-error.h>
41#include <lttng/tracker.h>
9f449915 42#include <common/string-utils/string-utils.h>
2f77fc4b
DG
43
44#include "channel.h"
45#include "consumer.h"
46#include "event.h"
8782cc74 47#include "health-sessiond.h"
2f77fc4b
DG
48#include "kernel.h"
49#include "kernel-consumer.h"
50#include "lttng-sessiond.h"
51#include "utils.h"
0dbc2034 52#include "lttng-syscall.h"
7c1d2758 53#include "agent.h"
93ec662e 54#include "buffer-registry.h"
b0880ae5
JG
55#include "notification-thread.h"
56#include "notification-thread-commands.h"
5c408ad8
JD
57#include "rotate.h"
58#include "rotation-thread.h"
8e319828 59#include "timer.h"
f28f9e44 60#include "agent-thread.h"
47aa1ca6 61#include "tracker.h"
2f77fc4b
DG
62
63#include "cmd.h"
64
a503e1ef
JG
65/* Sleep for 100ms between each check for the shm path's deletion. */
66#define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
67
3e3665b8
JG
68struct cmd_destroy_session_reply_context {
69 int reply_sock_fd;
70 bool implicit_rotation_on_destroy;
3285a971
JG
71 /*
72 * Indicates whether or not an error occurred while launching the
73 * destruction of a session.
74 */
75 enum lttng_error_code destruction_status;
3e3665b8
JG
76};
77
a503e1ef
JG
78static enum lttng_error_code wait_on_path(void *path);
79
80/*
81 * Command completion handler that is used by the destroy command
82 * when a session that has a non-default shm_path is being destroyed.
83 *
84 * See comment in cmd_destroy_session() for the rationale.
85 */
86static struct destroy_completion_handler {
87 struct cmd_completion_handler handler;
88 char shm_path[member_sizeof(struct ltt_session, shm_path)];
89} destroy_completion_handler = {
90 .handler = {
91 .run = wait_on_path,
92 .data = destroy_completion_handler.shm_path
93 },
94 .shm_path = { 0 },
95};
96
97static struct cmd_completion_handler *current_completion_handler;
98
2f77fc4b
DG
99/*
100 * Used to keep a unique index for each relayd socket created where this value
101 * is associated with streams on the consumer so it can match the right relayd
d88aee68
DG
102 * to send to. It must be accessed with the relayd_net_seq_idx_lock
103 * held.
2f77fc4b 104 */
d88aee68
DG
105static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
106static uint64_t relayd_net_seq_idx;
2f77fc4b 107
7076b56e
JG
108static int validate_ust_event_name(const char *);
109static int cmd_enable_event_internal(struct ltt_session *session,
df4f5a87 110 const struct lttng_domain *domain,
7076b56e
JG
111 char *channel_name, struct lttng_event *event,
112 char *filter_expression,
113 struct lttng_filter_bytecode *filter,
114 struct lttng_event_exclusion *exclusion,
115 int wpipe);
116
2f77fc4b
DG
117/*
118 * Create a session path used by list_lttng_sessions for the case that the
119 * session consumer is on the network.
120 */
121static int build_network_session_path(char *dst, size_t size,
122 struct ltt_session *session)
123{
124 int ret, kdata_port, udata_port;
125 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
126 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
127
128 assert(session);
129 assert(dst);
130
131 memset(tmp_urls, 0, sizeof(tmp_urls));
132 memset(tmp_uurl, 0, sizeof(tmp_uurl));
133
134 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
135
136 if (session->kernel_session && session->kernel_session->consumer) {
137 kuri = &session->kernel_session->consumer->dst.net.control;
138 kdata_port = session->kernel_session->consumer->dst.net.data.port;
139 }
140
141 if (session->ust_session && session->ust_session->consumer) {
142 uuri = &session->ust_session->consumer->dst.net.control;
143 udata_port = session->ust_session->consumer->dst.net.data.port;
144 }
145
146 if (uuri == NULL && kuri == NULL) {
147 uri = &session->consumer->dst.net.control;
148 kdata_port = session->consumer->dst.net.data.port;
149 } else if (kuri && uuri) {
150 ret = uri_compare(kuri, uuri);
151 if (ret) {
152 /* Not Equal */
153 uri = kuri;
154 /* Build uuri URL string */
155 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
156 if (ret < 0) {
157 goto error;
158 }
159 } else {
160 uri = kuri;
161 }
162 } else if (kuri && uuri == NULL) {
163 uri = kuri;
164 } else if (uuri && kuri == NULL) {
165 uri = uuri;
166 }
167
168 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
169 if (ret < 0) {
170 goto error;
171 }
172
9aa9f900
DG
173 /*
174 * Do we have a UST url set. If yes, this means we have both kernel and UST
175 * to print.
176 */
9d035200 177 if (*tmp_uurl != '\0') {
2f77fc4b
DG
178 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
179 tmp_urls, kdata_port, tmp_uurl, udata_port);
180 } else {
9aa9f900 181 int dport;
bef08707 182 if (kuri || (!kuri && !uuri)) {
9aa9f900
DG
183 dport = kdata_port;
184 } else {
185 /* No kernel URI, use the UST port. */
186 dport = udata_port;
187 }
188 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
2f77fc4b
DG
189 }
190
191error:
192 return ret;
193}
194
fb83fe64
JD
195/*
196 * Get run-time attributes if the session has been started (discarded events,
197 * lost packets).
198 */
199static int get_kernel_runtime_stats(struct ltt_session *session,
200 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
201 uint64_t *lost_packets)
202{
203 int ret;
204
205 if (!session->has_been_started) {
206 ret = 0;
207 *discarded_events = 0;
208 *lost_packets = 0;
209 goto end;
210 }
211
e1f3997a 212 ret = consumer_get_discarded_events(session->id, kchan->key,
fb83fe64
JD
213 session->kernel_session->consumer,
214 discarded_events);
215 if (ret < 0) {
216 goto end;
217 }
218
e1f3997a 219 ret = consumer_get_lost_packets(session->id, kchan->key,
fb83fe64
JD
220 session->kernel_session->consumer,
221 lost_packets);
222 if (ret < 0) {
223 goto end;
224 }
225
226end:
227 return ret;
228}
229
230/*
231 * Get run-time attributes if the session has been started (discarded events,
232 * lost packets).
233 */
234static int get_ust_runtime_stats(struct ltt_session *session,
235 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
236 uint64_t *lost_packets)
237{
238 int ret;
239 struct ltt_ust_session *usess;
240
a91c5803
JG
241 if (!discarded_events || !lost_packets) {
242 ret = -1;
243 goto end;
244 }
245
fb83fe64 246 usess = session->ust_session;
a905c624
JG
247 assert(discarded_events);
248 assert(lost_packets);
fb83fe64
JD
249
250 if (!usess || !session->has_been_started) {
251 *discarded_events = 0;
252 *lost_packets = 0;
253 ret = 0;
254 goto end;
255 }
256
257 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
258 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
259 &usess->buffer_reg_uid_list,
260 usess->consumer, uchan->id,
261 uchan->attr.overwrite,
262 discarded_events,
263 lost_packets);
264 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
265 ret = ust_app_pid_get_channel_runtime_stats(usess,
266 uchan, usess->consumer,
267 uchan->attr.overwrite,
268 discarded_events,
269 lost_packets);
270 if (ret < 0) {
271 goto end;
272 }
273 *discarded_events += uchan->per_pid_closed_app_discarded;
274 *lost_packets += uchan->per_pid_closed_app_lost;
275 } else {
276 ERR("Unsupported buffer type");
967bc289 277 assert(0);
fb83fe64
JD
278 ret = -1;
279 goto end;
280 }
281
282end:
283 return ret;
284}
285
2f77fc4b
DG
286/*
287 * Fill lttng_channel array of all channels.
288 */
d449df4a 289static ssize_t list_lttng_channels(enum lttng_domain_type domain,
fb83fe64 290 struct ltt_session *session, struct lttng_channel *channels,
cf0bcb51 291 struct lttng_channel_extended *chan_exts)
2f77fc4b 292{
d449df4a 293 int i = 0, ret = 0;
2f77fc4b
DG
294 struct ltt_kernel_channel *kchan;
295
296 DBG("Listing channels for session %s", session->name);
297
298 switch (domain) {
299 case LTTNG_DOMAIN_KERNEL:
300 /* Kernel channels */
301 if (session->kernel_session != NULL) {
302 cds_list_for_each_entry(kchan,
303 &session->kernel_session->channel_list.head, list) {
fb83fe64 304 uint64_t discarded_events, lost_packets;
cf0bcb51
JG
305 struct lttng_channel_extended *extended;
306
307 extended = (struct lttng_channel_extended *)
308 kchan->channel->attr.extended.ptr;
fb83fe64
JD
309
310 ret = get_kernel_runtime_stats(session, kchan,
311 &discarded_events, &lost_packets);
312 if (ret < 0) {
313 goto end;
314 }
2f77fc4b
DG
315 /* Copy lttng_channel struct to array */
316 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
317 channels[i].enabled = kchan->enabled;
fb83fe64
JD
318 chan_exts[i].discarded_events =
319 discarded_events;
320 chan_exts[i].lost_packets = lost_packets;
cf0bcb51
JG
321 chan_exts[i].monitor_timer_interval =
322 extended->monitor_timer_interval;
491d1539 323 chan_exts[i].blocking_timeout = 0;
2f77fc4b
DG
324 i++;
325 }
326 }
327 break;
328 case LTTNG_DOMAIN_UST:
329 {
330 struct lttng_ht_iter iter;
331 struct ltt_ust_channel *uchan;
332
e7fe706f 333 rcu_read_lock();
2f77fc4b
DG
334 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
335 &iter.iter, uchan, node.node) {
a91c5803 336 uint64_t discarded_events = 0, lost_packets = 0;
fb83fe64 337
8ee609c8
MD
338 if (lttng_strncpy(channels[i].name, uchan->name,
339 LTTNG_SYMBOL_NAME_LEN)) {
340 break;
341 }
2f77fc4b
DG
342 channels[i].attr.overwrite = uchan->attr.overwrite;
343 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
344 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
345 channels[i].attr.switch_timer_interval =
346 uchan->attr.switch_timer_interval;
347 channels[i].attr.read_timer_interval =
348 uchan->attr.read_timer_interval;
349 channels[i].enabled = uchan->enabled;
2f785fe7
DG
350 channels[i].attr.tracefile_size = uchan->tracefile_size;
351 channels[i].attr.tracefile_count = uchan->tracefile_count;
5e4435a4
JG
352
353 /*
354 * Map enum lttng_ust_output to enum lttng_event_output.
355 */
2f77fc4b
DG
356 switch (uchan->attr.output) {
357 case LTTNG_UST_MMAP:
2f77fc4b
DG
358 channels[i].attr.output = LTTNG_EVENT_MMAP;
359 break;
5e4435a4
JG
360 default:
361 /*
362 * LTTNG_UST_MMAP is the only supported UST
363 * output mode.
364 */
365 assert(0);
366 break;
2f77fc4b 367 }
fb83fe64 368
d449df4a
MD
369 chan_exts[i].monitor_timer_interval =
370 uchan->monitor_timer_interval;
491d1539
MD
371 chan_exts[i].blocking_timeout =
372 uchan->attr.u.s.blocking_timeout;
d449df4a 373
fb83fe64
JD
374 ret = get_ust_runtime_stats(session, uchan,
375 &discarded_events, &lost_packets);
376 if (ret < 0) {
377 break;
378 }
379 chan_exts[i].discarded_events = discarded_events;
380 chan_exts[i].lost_packets = lost_packets;
2f77fc4b
DG
381 i++;
382 }
e7fe706f 383 rcu_read_unlock();
2f77fc4b
DG
384 break;
385 }
386 default:
387 break;
388 }
fb83fe64
JD
389
390end:
d449df4a
MD
391 if (ret < 0) {
392 return -LTTNG_ERR_FATAL;
393 } else {
394 return LTTNG_OK;
395 }
2f77fc4b
DG
396}
397
3c02e545
FD
398static int append_extended_info(const char *filter_expression,
399 struct lttng_event_exclusion *exclusion,
400 struct lttng_userspace_probe_location *probe_location,
e368fb43 401 struct lttng_payload *payload)
b4e3ceb9 402{
3c02e545 403 int ret = 0;
b4e3ceb9 404 size_t filter_len = 0;
795d57ce 405 size_t nb_exclusions = 0;
3c02e545 406 size_t userspace_probe_location_len = 0;
e368fb43
JG
407 struct lttcomm_event_extended_header extended_header = {};
408 struct lttcomm_event_extended_header *p_extended_header;
409 const size_t original_payload_size = payload->buffer.size;
410
411 ret = lttng_dynamic_buffer_append(&payload->buffer, &extended_header,
412 sizeof(extended_header));
413 if (ret) {
414 goto end;
415 }
b4e3ceb9
PP
416
417 if (filter_expression) {
418 filter_len = strlen(filter_expression) + 1;
e368fb43
JG
419 ret = lttng_dynamic_buffer_append(&payload->buffer,
420 filter_expression, filter_len);
421 if (ret) {
422 goto end;
423 }
b4e3ceb9
PP
424 }
425
795d57ce 426 if (exclusion) {
e368fb43
JG
427 const size_t len = exclusion->count * LTTNG_SYMBOL_NAME_LEN;
428
795d57ce 429 nb_exclusions = exclusion->count;
e368fb43
JG
430
431 ret = lttng_dynamic_buffer_append(
432 &payload->buffer, &exclusion->names, len);
433 if (ret) {
434 goto end;
435 }
795d57ce
PP
436 }
437
3c02e545 438 if (probe_location) {
e368fb43
JG
439 const size_t size_before_probe = payload->buffer.size;
440
3c02e545 441 ret = lttng_userspace_probe_location_serialize(probe_location,
e368fb43 442 payload);
3c02e545
FD
443 if (ret < 0) {
444 ret = -1;
445 goto end;
446 }
795d57ce 447
e368fb43
JG
448 userspace_probe_location_len =
449 payload->buffer.size - size_before_probe;
795d57ce
PP
450 }
451
e368fb43
JG
452 /* Set header fields */
453 p_extended_header = (struct lttcomm_event_extended_header *)
454 (payload->buffer.data + original_payload_size);
795d57ce 455
e368fb43
JG
456 p_extended_header->filter_len = filter_len;
457 p_extended_header->nb_exclusions = nb_exclusions;
458 p_extended_header->userspace_probe_location_len =
459 userspace_probe_location_len;
3c02e545 460
3c02e545
FD
461 ret = 0;
462end:
463 return ret;
b4e3ceb9
PP
464}
465
3c6a091f 466/*
022d91ba 467 * Create a list of agent domain events.
3c6a091f
DG
468 *
469 * Return number of events in list on success or else a negative value.
470 */
022d91ba 471static int list_lttng_agent_events(struct agent *agt,
e368fb43 472 struct lttng_payload *payload)
3c6a091f 473{
e368fb43
JG
474 int nb_events = 0, ret = 0;
475 const struct agent_event *agent_event;
3c6a091f
DG
476 struct lttng_ht_iter iter;
477
022d91ba 478 assert(agt);
3c6a091f 479
022d91ba 480 DBG3("Listing agent events");
3c6a091f 481
e5bbf678 482 rcu_read_lock();
e368fb43
JG
483 cds_lfht_for_each_entry (
484 agt->events->ht, &iter.iter, agent_event, node.node) {
485 struct lttng_event event = {
486 .enabled = agent_event->enabled,
487 .loglevel = agent_event->loglevel_value,
488 .loglevel_type = agent_event->loglevel_type,
489 };
490
43ed1485
JG
491 ret = lttng_strncpy(event.name, agent_event->name, sizeof(event.name));
492 if (ret) {
493 /* Internal error, invalid name. */
494 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
495 agent_event->name, sizeof(event.name));
496 ret = -LTTNG_ERR_UNK;
497 goto end;
498 }
e368fb43
JG
499
500 ret = lttng_dynamic_buffer_append(
501 &payload->buffer, &event, sizeof(event));
3c02e545 502 if (ret) {
e368fb43
JG
503 ERR("Failed to append event to payload");
504 ret = -LTTNG_ERR_NOMEM;
505 goto end;
3c02e545 506 }
b4e3ceb9 507
e368fb43 508 nb_events++;
3c6a091f
DG
509 }
510
e368fb43
JG
511 cds_lfht_for_each_entry (
512 agt->events->ht, &iter.iter, agent_event, node.node) {
513 /* Append extended info. */
514 ret = append_extended_info(agent_event->filter_expression, NULL,
515 NULL, payload);
3c02e545 516 if (ret) {
e368fb43
JG
517 ERR("Failed to append extended event info to payload");
518 ret = -LTTNG_ERR_NOMEM;
519 goto end;
3c02e545 520 }
3c6a091f 521 }
3c6a091f 522
e368fb43 523 ret = nb_events;
47e52862 524end:
3c02e545 525 rcu_read_unlock();
3c6a091f
DG
526 return ret;
527}
528
2f77fc4b
DG
529/*
530 * Create a list of ust global domain events.
531 */
532static int list_lttng_ust_global_events(char *channel_name,
b4e3ceb9 533 struct ltt_ust_domain_global *ust_global,
e368fb43 534 struct lttng_payload *payload)
2f77fc4b 535{
e368fb43
JG
536 int ret = 0;
537 unsigned int nb_events = 0;
2f77fc4b 538 struct lttng_ht_iter iter;
e368fb43
JG
539 const struct lttng_ht_node_str *node;
540 const struct ltt_ust_channel *uchan;
541 const struct ltt_ust_event *uevent;
2f77fc4b
DG
542
543 DBG("Listing UST global events for channel %s", channel_name);
544
545 rcu_read_lock();
546
e368fb43 547 lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
2f77fc4b
DG
548 node = lttng_ht_iter_get_node_str(&iter);
549 if (node == NULL) {
f73fabfd 550 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
d31d3e8c 551 goto end;
2f77fc4b
DG
552 }
553
554 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
555
e368fb43 556 DBG3("Listing UST global events");
2f77fc4b 557
b4e3ceb9 558 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
e368fb43
JG
559 struct lttng_event event = {};
560
b4e3ceb9 561 if (uevent->internal) {
b4e3ceb9
PP
562 continue;
563 }
564
43ed1485
JG
565 ret = lttng_strncpy(event.name, uevent->attr.name, sizeof(event.name));
566 if (ret) {
567 /* Internal error, invalid name. */
568 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
569 uevent->attr.name, sizeof(event.name));
570 ret = -LTTNG_ERR_UNK;
571 goto end;
572 }
2f77fc4b 573
e368fb43 574 event.enabled = uevent->enabled;
2f77fc4b
DG
575
576 switch (uevent->attr.instrumentation) {
577 case LTTNG_UST_TRACEPOINT:
e368fb43 578 event.type = LTTNG_EVENT_TRACEPOINT;
2f77fc4b
DG
579 break;
580 case LTTNG_UST_PROBE:
e368fb43 581 event.type = LTTNG_EVENT_PROBE;
2f77fc4b
DG
582 break;
583 case LTTNG_UST_FUNCTION:
e368fb43 584 event.type = LTTNG_EVENT_FUNCTION;
2f77fc4b
DG
585 break;
586 }
587
e368fb43 588 event.loglevel = uevent->attr.loglevel;
2f77fc4b
DG
589 switch (uevent->attr.loglevel_type) {
590 case LTTNG_UST_LOGLEVEL_ALL:
e368fb43 591 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2f77fc4b
DG
592 break;
593 case LTTNG_UST_LOGLEVEL_RANGE:
e368fb43 594 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
2f77fc4b
DG
595 break;
596 case LTTNG_UST_LOGLEVEL_SINGLE:
e368fb43 597 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
2f77fc4b
DG
598 break;
599 }
e368fb43 600
2f77fc4b 601 if (uevent->filter) {
e368fb43 602 event.filter = 1;
2f77fc4b 603 }
e368fb43 604
4634f12e 605 if (uevent->exclusion) {
e368fb43
JG
606 event.exclusion = 1;
607 }
608
609 ret = lttng_dynamic_buffer_append(&payload->buffer, &event, sizeof(event));
610 if (ret) {
611 ERR("Failed to append event to payload");
612 ret = -LTTNG_ERR_NOMEM;
613 goto end;
4634f12e 614 }
b4e3ceb9 615
e368fb43
JG
616 nb_events++;
617 }
618
619 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
620 /* Append extended info. */
3c02e545 621 ret = append_extended_info(uevent->filter_expression,
e368fb43 622 uevent->exclusion, NULL, payload);
3c02e545 623 if (ret) {
e368fb43 624 ERR("Failed to append extended event info to payload");
3c02e545
FD
625 ret = -LTTNG_ERR_FATAL;
626 goto end;
627 }
2f77fc4b
DG
628 }
629
e368fb43 630 ret = nb_events;
d31d3e8c 631end:
2f77fc4b
DG
632 rcu_read_unlock();
633 return ret;
634}
635
636/*
637 * Fill lttng_event array of all kernel events in the channel.
638 */
639static int list_lttng_kernel_events(char *channel_name,
b4e3ceb9 640 struct ltt_kernel_session *kernel_session,
e368fb43 641 struct lttng_payload *payload)
2f77fc4b 642{
e368fb43 643 int ret;
2f77fc4b 644 unsigned int nb_event;
e368fb43
JG
645 const struct ltt_kernel_event *kevent;
646 const struct ltt_kernel_channel *kchan;
2f77fc4b
DG
647
648 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
649 if (kchan == NULL) {
f73fabfd 650 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
651 goto error;
652 }
653
654 nb_event = kchan->event_count;
655
656 DBG("Listing events for channel %s", kchan->channel->name);
657
e368fb43
JG
658 /* Kernel channels */
659 cds_list_for_each_entry(kevent, &kchan->events_list.head , list) {
660 struct lttng_event event = {};
2f77fc4b 661
43ed1485
JG
662 ret = lttng_strncpy(event.name, kevent->event->name, sizeof(event.name));
663 if (ret) {
664 /* Internal error, invalid name. */
665 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
666 kevent->event->name,
667 sizeof(event.name));
668 ret = -LTTNG_ERR_UNK;
669 goto end;
670 }
671
e368fb43
JG
672 event.enabled = kevent->enabled;
673 event.filter = (unsigned char) !!kevent->filter_expression;
b4e3ceb9 674
e368fb43 675 switch (kevent->event->instrumentation) {
2f77fc4b 676 case LTTNG_KERNEL_TRACEPOINT:
e368fb43 677 event.type = LTTNG_EVENT_TRACEPOINT;
2f77fc4b 678 break;
2f77fc4b 679 case LTTNG_KERNEL_KRETPROBE:
e368fb43
JG
680 event.type = LTTNG_EVENT_FUNCTION;
681 memcpy(&event.attr.probe, &kevent->event->u.kprobe,
1896972b
DG
682 sizeof(struct lttng_kernel_kprobe));
683 break;
684 case LTTNG_KERNEL_KPROBE:
e368fb43
JG
685 event.type = LTTNG_EVENT_PROBE;
686 memcpy(&event.attr.probe, &kevent->event->u.kprobe,
2f77fc4b
DG
687 sizeof(struct lttng_kernel_kprobe));
688 break;
b955b4d4 689 case LTTNG_KERNEL_UPROBE:
e368fb43 690 event.type = LTTNG_EVENT_USERSPACE_PROBE;
b955b4d4 691 break;
2f77fc4b 692 case LTTNG_KERNEL_FUNCTION:
e368fb43
JG
693 event.type = LTTNG_EVENT_FUNCTION;
694 memcpy(&event.attr.ftrace, &kevent->event->u.ftrace,
2f77fc4b
DG
695 sizeof(struct lttng_kernel_function));
696 break;
697 case LTTNG_KERNEL_NOOP:
e368fb43 698 event.type = LTTNG_EVENT_NOOP;
2f77fc4b
DG
699 break;
700 case LTTNG_KERNEL_SYSCALL:
e368fb43 701 event.type = LTTNG_EVENT_SYSCALL;
2f77fc4b
DG
702 break;
703 case LTTNG_KERNEL_ALL:
1ab8c2ad
FD
704 /* fall-through. */
705 default:
2f77fc4b
DG
706 assert(0);
707 break;
708 }
b4e3ceb9 709
e368fb43
JG
710 ret = lttng_dynamic_buffer_append(
711 &payload->buffer, &event, sizeof(event));
712 if (ret) {
713 ERR("Failed to append event to payload");
714 ret = -LTTNG_ERR_NOMEM;
715 goto end;
716 }
717 }
718
719 cds_list_for_each_entry(kevent, &kchan->events_list.head , list) {
720 /* Append extended info. */
721 ret = append_extended_info(kevent->filter_expression, NULL,
722 kevent->userspace_probe_location, payload);
3c02e545
FD
723 if (ret) {
724 DBG("Error appending extended info message");
725 ret = -LTTNG_ERR_FATAL;
726 goto error;
727 }
2f77fc4b
DG
728 }
729
db906c12 730end:
2f77fc4b 731 return nb_event;
2f77fc4b 732error:
6314496f 733 return ret;
2f77fc4b
DG
734}
735
736/*
737 * Add URI so the consumer output object. Set the correct path depending on the
738 * domain adding the default trace directory.
739 */
b178f53e
JG
740static enum lttng_error_code add_uri_to_consumer(
741 const struct ltt_session *session,
742 struct consumer_output *consumer,
743 struct lttng_uri *uri, enum lttng_domain_type domain)
2f77fc4b 744{
b178f53e
JG
745 int ret;
746 enum lttng_error_code ret_code = LTTNG_OK;
2f77fc4b
DG
747
748 assert(uri);
749
750 if (consumer == NULL) {
751 DBG("No consumer detected. Don't add URI. Stopping.");
b178f53e 752 ret_code = LTTNG_ERR_NO_CONSUMER;
2f77fc4b
DG
753 goto error;
754 }
755
756 switch (domain) {
757 case LTTNG_DOMAIN_KERNEL:
b178f53e
JG
758 ret = lttng_strncpy(consumer->domain_subdir,
759 DEFAULT_KERNEL_TRACE_DIR,
760 sizeof(consumer->domain_subdir));
2f77fc4b
DG
761 break;
762 case LTTNG_DOMAIN_UST:
b178f53e
JG
763 ret = lttng_strncpy(consumer->domain_subdir,
764 DEFAULT_UST_TRACE_DIR,
765 sizeof(consumer->domain_subdir));
2f77fc4b
DG
766 break;
767 default:
768 /*
b178f53e
JG
769 * This case is possible is we try to add the URI to the global
770 * tracing session consumer object which in this case there is
771 * no subdir.
2f77fc4b 772 */
b178f53e
JG
773 memset(consumer->domain_subdir, 0,
774 sizeof(consumer->domain_subdir));
775 ret = 0;
776 }
777 if (ret) {
778 ERR("Failed to initialize consumer output domain subdirectory");
779 ret_code = LTTNG_ERR_FATAL;
780 goto error;
2f77fc4b
DG
781 }
782
783 switch (uri->dtype) {
784 case LTTNG_DST_IPV4:
785 case LTTNG_DST_IPV6:
786 DBG2("Setting network URI to consumer");
787
df75acac
DG
788 if (consumer->type == CONSUMER_DST_NET) {
789 if ((uri->stype == LTTNG_STREAM_CONTROL &&
785d2d0d
DG
790 consumer->dst.net.control_isset) ||
791 (uri->stype == LTTNG_STREAM_DATA &&
792 consumer->dst.net.data_isset)) {
b178f53e 793 ret_code = LTTNG_ERR_URL_EXIST;
df75acac
DG
794 goto error;
795 }
796 } else {
b178f53e 797 memset(&consumer->dst, 0, sizeof(consumer->dst));
785d2d0d
DG
798 }
799
2f77fc4b 800 /* Set URI into consumer output object */
b178f53e 801 ret = consumer_set_network_uri(session, consumer, uri);
2f77fc4b 802 if (ret < 0) {
b178f53e 803 ret_code = -ret;
2f77fc4b
DG
804 goto error;
805 } else if (ret == 1) {
806 /*
807 * URI was the same in the consumer so we do not append the subdir
808 * again so to not duplicate output dir.
809 */
b178f53e 810 ret_code = LTTNG_OK;
2f77fc4b
DG
811 goto error;
812 }
2f77fc4b
DG
813 break;
814 case LTTNG_DST_PATH:
b178f53e
JG
815 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
816 ret_code = LTTNG_ERR_INVALID;
9ac05d92
MD
817 goto error;
818 }
b178f53e
JG
819 DBG2("Setting trace directory path from URI to %s",
820 uri->dst.path);
821 memset(&consumer->dst, 0, sizeof(consumer->dst));
822
823 ret = lttng_strncpy(consumer->dst.session_root_path,
824 uri->dst.path,
825 sizeof(consumer->dst.session_root_path));
4df41cad
JG
826 if (ret) {
827 ret_code = LTTNG_ERR_FATAL;
828 goto error;
829 }
2f77fc4b
DG
830 consumer->type = CONSUMER_DST_LOCAL;
831 break;
832 }
833
b178f53e 834 ret_code = LTTNG_OK;
2f77fc4b 835error:
b178f53e 836 return ret_code;
2f77fc4b
DG
837}
838
839/*
840 * Init tracing by creating trace directory and sending fds kernel consumer.
841 */
842static int init_kernel_tracing(struct ltt_kernel_session *session)
843{
844 int ret = 0;
845 struct lttng_ht_iter iter;
846 struct consumer_socket *socket;
847
848 assert(session);
849
e7fe706f
DG
850 rcu_read_lock();
851
2f77fc4b
DG
852 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
853 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
854 socket, node.node) {
2f77fc4b 855 pthread_mutex_lock(socket->lock);
f50f23d9 856 ret = kernel_consumer_send_session(socket, session);
2f77fc4b
DG
857 pthread_mutex_unlock(socket->lock);
858 if (ret < 0) {
f73fabfd 859 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
860 goto error;
861 }
862 }
863 }
864
865error:
e7fe706f 866 rcu_read_unlock();
2f77fc4b
DG
867 return ret;
868}
869
870/*
871 * Create a socket to the relayd using the URI.
872 *
873 * On success, the relayd_sock pointer is set to the created socket.
9a654598 874 * Else, it remains untouched and an LTTng error code is returned.
2f77fc4b 875 */
9a654598 876static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
b31610f2
JD
877 struct lttcomm_relayd_sock **relayd_sock,
878 struct consumer_output *consumer)
2f77fc4b
DG
879{
880 int ret;
9a654598 881 enum lttng_error_code status = LTTNG_OK;
6151a90f 882 struct lttcomm_relayd_sock *rsock;
2f77fc4b 883
6151a90f
JD
884 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
885 RELAYD_VERSION_COMM_MINOR);
886 if (!rsock) {
9a654598 887 status = LTTNG_ERR_FATAL;
2f77fc4b
DG
888 goto error;
889 }
890
ffe60014
DG
891 /*
892 * Connect to relayd so we can proceed with a session creation. This call
893 * can possibly block for an arbitrary amount of time to set the health
894 * state to be in poll execution.
895 */
896 health_poll_entry();
6151a90f 897 ret = relayd_connect(rsock);
ffe60014 898 health_poll_exit();
2f77fc4b
DG
899 if (ret < 0) {
900 ERR("Unable to reach lttng-relayd");
9a654598 901 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
902 goto free_sock;
903 }
904
905 /* Create socket for control stream. */
906 if (uri->stype == LTTNG_STREAM_CONTROL) {
eacb7b6f
MD
907 uint64_t result_flags;
908
2f77fc4b
DG
909 DBG3("Creating relayd stream socket from URI");
910
911 /* Check relayd version */
6151a90f 912 ret = relayd_version_check(rsock);
67d5aa28 913 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
9a654598 914 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
67d5aa28
JD
915 goto close_sock;
916 } else if (ret < 0) {
917 ERR("Unable to reach lttng-relayd");
9a654598 918 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
919 goto close_sock;
920 }
b31610f2
JD
921 consumer->relay_major_version = rsock->major;
922 consumer->relay_minor_version = rsock->minor;
eacb7b6f
MD
923 ret = relayd_get_configuration(rsock, 0,
924 &result_flags);
925 if (ret < 0) {
926 ERR("Unable to get relayd configuration");
927 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
928 goto close_sock;
929 }
930 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
931 consumer->relay_allows_clear = true;
932 }
2f77fc4b
DG
933 } else if (uri->stype == LTTNG_STREAM_DATA) {
934 DBG3("Creating relayd data socket from URI");
935 } else {
936 /* Command is not valid */
937 ERR("Relayd invalid stream type: %d", uri->stype);
9a654598 938 status = LTTNG_ERR_INVALID;
2f77fc4b
DG
939 goto close_sock;
940 }
941
6151a90f 942 *relayd_sock = rsock;
2f77fc4b 943
9a654598 944 return status;
2f77fc4b
DG
945
946close_sock:
6151a90f
JD
947 /* The returned value is not useful since we are on an error path. */
948 (void) relayd_close(rsock);
2f77fc4b 949free_sock:
6151a90f 950 free(rsock);
2f77fc4b 951error:
9a654598 952 return status;
2f77fc4b
DG
953}
954
955/*
956 * Connect to the relayd using URI and send the socket to the right consumer.
43fade62
JG
957 *
958 * The consumer socket lock must be held by the caller.
9a654598
JG
959 *
960 * Returns LTTNG_OK on success or an LTTng error code on failure.
2f77fc4b 961 */
9a654598
JG
962static enum lttng_error_code send_consumer_relayd_socket(
963 unsigned int session_id,
3044f922 964 struct lttng_uri *relayd_uri,
56a37563 965 struct consumer_output *consumer,
d3e2ba59 966 struct consumer_socket *consumer_sock,
fb9a95c4 967 const char *session_name, const char *hostname,
6fa5fe7c 968 const char *base_path, int session_live_timer,
0e270a1e 969 const uint64_t *current_chunk_id,
46ef2188
MD
970 time_t session_creation_time,
971 bool session_name_contains_creation_time)
2f77fc4b
DG
972{
973 int ret;
6151a90f 974 struct lttcomm_relayd_sock *rsock = NULL;
9a654598 975 enum lttng_error_code status;
2f77fc4b 976
ffe60014 977 /* Connect to relayd and make version check if uri is the control. */
9a654598
JG
978 status = create_connect_relayd(relayd_uri, &rsock, consumer);
979 if (status != LTTNG_OK) {
9e218353 980 goto relayd_comm_error;
ffe60014 981 }
6151a90f 982 assert(rsock);
ffe60014 983
2f77fc4b 984 /* Set the network sequence index if not set. */
d88aee68
DG
985 if (consumer->net_seq_index == (uint64_t) -1ULL) {
986 pthread_mutex_lock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
987 /*
988 * Increment net_seq_idx because we are about to transfer the
989 * new relayd socket to the consumer.
d88aee68 990 * Assign unique key so the consumer can match streams.
2f77fc4b 991 */
d88aee68
DG
992 consumer->net_seq_index = ++relayd_net_seq_idx;
993 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
994 }
995
2f77fc4b 996 /* Send relayd socket to consumer. */
6151a90f 997 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
d3e2ba59 998 relayd_uri->stype, session_id,
6fa5fe7c
MD
999 session_name, hostname, base_path,
1000 session_live_timer, current_chunk_id,
46ef2188 1001 session_creation_time, session_name_contains_creation_time);
2f77fc4b 1002 if (ret < 0) {
9a654598 1003 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2f77fc4b
DG
1004 goto close_sock;
1005 }
1006
c890b720
DG
1007 /* Flag that the corresponding socket was sent. */
1008 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
ffe60014 1009 consumer_sock->control_sock_sent = 1;
c890b720 1010 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
ffe60014 1011 consumer_sock->data_sock_sent = 1;
c890b720
DG
1012 }
1013
2f77fc4b
DG
1014 /*
1015 * Close socket which was dup on the consumer side. The session daemon does
1016 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1017 */
1018
1019close_sock:
9a654598 1020 if (status != LTTNG_OK) {
ffe60014 1021 /*
d9078d0c
DG
1022 * The consumer output for this session should not be used anymore
1023 * since the relayd connection failed thus making any tracing or/and
1024 * streaming not usable.
ffe60014 1025 */
d9078d0c 1026 consumer->enabled = 0;
ffe60014 1027 }
9e218353
JR
1028 (void) relayd_close(rsock);
1029 free(rsock);
1030
1031relayd_comm_error:
9a654598 1032 return status;
2f77fc4b
DG
1033}
1034
1035/*
1036 * Send both relayd sockets to a specific consumer and domain. This is a
1037 * helper function to facilitate sending the information to the consumer for a
1038 * session.
43fade62
JG
1039 *
1040 * The consumer socket lock must be held by the caller.
9a654598
JG
1041 *
1042 * Returns LTTNG_OK, or an LTTng error code on failure.
2f77fc4b 1043 */
9a654598
JG
1044static enum lttng_error_code send_consumer_relayd_sockets(
1045 enum lttng_domain_type domain,
56a37563 1046 unsigned int session_id, struct consumer_output *consumer,
fb9a95c4 1047 struct consumer_socket *sock, const char *session_name,
6fa5fe7c 1048 const char *hostname, const char *base_path, int session_live_timer,
46ef2188
MD
1049 const uint64_t *current_chunk_id, time_t session_creation_time,
1050 bool session_name_contains_creation_time)
2f77fc4b 1051{
9a654598 1052 enum lttng_error_code status = LTTNG_OK;
2f77fc4b 1053
2f77fc4b 1054 assert(consumer);
6dc3064a 1055 assert(sock);
2f77fc4b 1056
2f77fc4b 1057 /* Sending control relayd socket. */
ffe60014 1058 if (!sock->control_sock_sent) {
9a654598 1059 status = send_consumer_relayd_socket(session_id,
d3e2ba59 1060 &consumer->dst.net.control, consumer, sock,
6fa5fe7c 1061 session_name, hostname, base_path, session_live_timer,
46ef2188
MD
1062 current_chunk_id, session_creation_time,
1063 session_name_contains_creation_time);
9a654598 1064 if (status != LTTNG_OK) {
c890b720
DG
1065 goto error;
1066 }
2f77fc4b
DG
1067 }
1068
1069 /* Sending data relayd socket. */
ffe60014 1070 if (!sock->data_sock_sent) {
9a654598 1071 status = send_consumer_relayd_socket(session_id,
d3e2ba59 1072 &consumer->dst.net.data, consumer, sock,
6fa5fe7c 1073 session_name, hostname, base_path, session_live_timer,
46ef2188
MD
1074 current_chunk_id, session_creation_time,
1075 session_name_contains_creation_time);
9a654598 1076 if (status != LTTNG_OK) {
c890b720
DG
1077 goto error;
1078 }
2f77fc4b
DG
1079 }
1080
2f77fc4b 1081error:
9a654598 1082 return status;
2f77fc4b
DG
1083}
1084
1085/*
1086 * Setup relayd connections for a tracing session. First creates the socket to
1087 * the relayd and send them to the right domain consumer. Consumer type MUST be
1088 * network.
1089 */
ffe60014 1090int cmd_setup_relayd(struct ltt_session *session)
2f77fc4b 1091{
f73fabfd 1092 int ret = LTTNG_OK;
2f77fc4b
DG
1093 struct ltt_ust_session *usess;
1094 struct ltt_kernel_session *ksess;
1095 struct consumer_socket *socket;
1096 struct lttng_ht_iter iter;
0e270a1e 1097 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
2f77fc4b 1098
0e270a1e 1099 assert(session);
2f77fc4b
DG
1100
1101 usess = session->ust_session;
1102 ksess = session->kernel_session;
1103
785d2d0d 1104 DBG("Setting relayd for session %s", session->name);
2f77fc4b 1105
aa997ea3 1106 rcu_read_lock();
1e791a74
JG
1107 if (session->current_trace_chunk) {
1108 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1109 session->current_trace_chunk, &current_chunk_id.value);
1110
1111 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1112 current_chunk_id.is_set = true;
1113 } else {
1114 ERR("Failed to get current trace chunk id");
1115 ret = LTTNG_ERR_UNK;
1116 goto error;
1117 }
1118 }
1119
2f77fc4b
DG
1120 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1121 && usess->consumer->enabled) {
1122 /* For each consumer socket, send relayd sockets */
1123 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1124 socket, node.node) {
2f77fc4b 1125 pthread_mutex_lock(socket->lock);
6dc3064a 1126 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
d3e2ba59
JD
1127 usess->consumer, socket,
1128 session->name, session->hostname,
6fa5fe7c 1129 session->base_path,
1e791a74 1130 session->live_timer,
db1da059 1131 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
46ef2188
MD
1132 session->creation_time,
1133 session->name_contains_creation_time);
2f77fc4b 1134 pthread_mutex_unlock(socket->lock);
f73fabfd 1135 if (ret != LTTNG_OK) {
2f77fc4b
DG
1136 goto error;
1137 }
6dc3064a
DG
1138 /* Session is now ready for network streaming. */
1139 session->net_handle = 1;
2f77fc4b 1140 }
b31610f2
JD
1141 session->consumer->relay_major_version =
1142 usess->consumer->relay_major_version;
1143 session->consumer->relay_minor_version =
1144 usess->consumer->relay_minor_version;
eacb7b6f
MD
1145 session->consumer->relay_allows_clear =
1146 usess->consumer->relay_allows_clear;
2f77fc4b
DG
1147 }
1148
1149 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1150 && ksess->consumer->enabled) {
1151 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1152 socket, node.node) {
2f77fc4b 1153 pthread_mutex_lock(socket->lock);
6dc3064a 1154 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
d3e2ba59
JD
1155 ksess->consumer, socket,
1156 session->name, session->hostname,
6fa5fe7c 1157 session->base_path,
1e791a74 1158 session->live_timer,
db1da059 1159 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
46ef2188
MD
1160 session->creation_time,
1161 session->name_contains_creation_time);
2f77fc4b 1162 pthread_mutex_unlock(socket->lock);
f73fabfd 1163 if (ret != LTTNG_OK) {
2f77fc4b
DG
1164 goto error;
1165 }
6dc3064a
DG
1166 /* Session is now ready for network streaming. */
1167 session->net_handle = 1;
2f77fc4b 1168 }
b31610f2
JD
1169 session->consumer->relay_major_version =
1170 ksess->consumer->relay_major_version;
1171 session->consumer->relay_minor_version =
1172 ksess->consumer->relay_minor_version;
eacb7b6f
MD
1173 session->consumer->relay_allows_clear =
1174 ksess->consumer->relay_allows_clear;
2f77fc4b
DG
1175 }
1176
1177error:
e7fe706f 1178 rcu_read_unlock();
2f77fc4b
DG
1179 return ret;
1180}
1181
9b6c7ec5
DG
1182/*
1183 * Start a kernel session by opening all necessary streams.
1184 */
4dbe1875 1185int start_kernel_session(struct ltt_kernel_session *ksess)
9b6c7ec5
DG
1186{
1187 int ret;
1188 struct ltt_kernel_channel *kchan;
1189
1190 /* Open kernel metadata */
07b86b52 1191 if (ksess->metadata == NULL && ksess->output_traces) {
9b6c7ec5
DG
1192 ret = kernel_open_metadata(ksess);
1193 if (ret < 0) {
1194 ret = LTTNG_ERR_KERN_META_FAIL;
1195 goto error;
1196 }
1197 }
1198
1199 /* Open kernel metadata stream */
07b86b52 1200 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
9b6c7ec5
DG
1201 ret = kernel_open_metadata_stream(ksess);
1202 if (ret < 0) {
1203 ERR("Kernel create metadata stream failed");
1204 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1205 goto error;
1206 }
1207 }
1208
1209 /* For each channel */
1210 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1211 if (kchan->stream_count == 0) {
1212 ret = kernel_open_channel_stream(kchan);
1213 if (ret < 0) {
1214 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1215 goto error;
1216 }
1217 /* Update the stream global counter */
1218 ksess->stream_count_global += ret;
1219 }
1220 }
1221
1222 /* Setup kernel consumer socket and send fds to it */
1223 ret = init_kernel_tracing(ksess);
e43c41c5 1224 if (ret != 0) {
9b6c7ec5
DG
1225 ret = LTTNG_ERR_KERN_START_FAIL;
1226 goto error;
1227 }
1228
1229 /* This start the kernel tracing */
1230 ret = kernel_start_session(ksess);
1231 if (ret < 0) {
1232 ret = LTTNG_ERR_KERN_START_FAIL;
1233 goto error;
1234 }
1235
1236 /* Quiescent wait after starting trace */
7d268848 1237 kernel_wait_quiescent();
9b6c7ec5 1238
14fb1ebe 1239 ksess->active = 1;
9b6c7ec5
DG
1240
1241 ret = LTTNG_OK;
1242
1243error:
1244 return ret;
1245}
1246
4dbe1875
MD
1247int stop_kernel_session(struct ltt_kernel_session *ksess)
1248{
1249 struct ltt_kernel_channel *kchan;
1250 bool error_occurred = false;
1251 int ret;
1252
1253 if (!ksess || !ksess->active) {
1254 return LTTNG_OK;
1255 }
1256 DBG("Stopping kernel tracing");
1257
1258 ret = kernel_stop_session(ksess);
1259 if (ret < 0) {
1260 ret = LTTNG_ERR_KERN_STOP_FAIL;
1261 goto error;
1262 }
1263
1264 kernel_wait_quiescent();
1265
1266 /* Flush metadata after stopping (if exists) */
1267 if (ksess->metadata_stream_fd >= 0) {
1268 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1269 if (ret < 0) {
1270 ERR("Kernel metadata flush failed");
1271 error_occurred = true;
1272 }
1273 }
1274
1275 /* Flush all buffers after stopping */
1276 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1277 ret = kernel_flush_buffer(kchan);
1278 if (ret < 0) {
1279 ERR("Kernel flush buffer error");
1280 error_occurred = true;
1281 }
1282 }
1283
1284 ksess->active = 0;
1285 if (error_occurred) {
1286 ret = LTTNG_ERR_UNK;
1287 } else {
1288 ret = LTTNG_OK;
1289 }
1290error:
1291 return ret;
1292}
1293
2f77fc4b
DG
1294/*
1295 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1296 */
56a37563
JG
1297int cmd_disable_channel(struct ltt_session *session,
1298 enum lttng_domain_type domain, char *channel_name)
2f77fc4b
DG
1299{
1300 int ret;
1301 struct ltt_ust_session *usess;
1302
1303 usess = session->ust_session;
1304
2223c96f
DG
1305 rcu_read_lock();
1306
2f77fc4b
DG
1307 switch (domain) {
1308 case LTTNG_DOMAIN_KERNEL:
1309 {
1310 ret = channel_kernel_disable(session->kernel_session,
1311 channel_name);
f73fabfd 1312 if (ret != LTTNG_OK) {
2f77fc4b
DG
1313 goto error;
1314 }
1315
7d268848 1316 kernel_wait_quiescent();
2f77fc4b
DG
1317 break;
1318 }
1319 case LTTNG_DOMAIN_UST:
1320 {
1321 struct ltt_ust_channel *uchan;
1322 struct lttng_ht *chan_ht;
1323
1324 chan_ht = usess->domain_global.channels;
1325
1326 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1327 if (uchan == NULL) {
f73fabfd 1328 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1329 goto error;
1330 }
1331
7972aab2 1332 ret = channel_ust_disable(usess, uchan);
f73fabfd 1333 if (ret != LTTNG_OK) {
2f77fc4b
DG
1334 goto error;
1335 }
1336 break;
1337 }
2f77fc4b 1338 default:
f73fabfd 1339 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1340 goto error;
1341 }
1342
f73fabfd 1343 ret = LTTNG_OK;
2f77fc4b
DG
1344
1345error:
2223c96f 1346 rcu_read_unlock();
2f77fc4b
DG
1347 return ret;
1348}
1349
1350/*
1351 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1352 *
1353 * The wpipe arguments is used as a notifier for the kernel thread.
1354 */
1355int cmd_enable_channel(struct ltt_session *session,
df4f5a87 1356 const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe)
2f77fc4b
DG
1357{
1358 int ret;
1359 struct ltt_ust_session *usess = session->ust_session;
1360 struct lttng_ht *chan_ht;
1f345e94 1361 size_t len;
df4f5a87 1362 struct lttng_channel attr;
2f77fc4b
DG
1363
1364 assert(session);
df4f5a87 1365 assert(_attr);
7972aab2 1366 assert(domain);
2f77fc4b 1367
df4f5a87
JG
1368 attr = *_attr;
1369 len = lttng_strnlen(attr.name, sizeof(attr.name));
1f345e94
PP
1370
1371 /* Validate channel name */
df4f5a87
JG
1372 if (attr.name[0] == '.' ||
1373 memchr(attr.name, '/', len) != NULL) {
1f345e94
PP
1374 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1375 goto end;
1376 }
1377
df4f5a87 1378 DBG("Enabling channel %s for session %s", attr.name, session->name);
2f77fc4b 1379
03b4fdcf
DG
1380 rcu_read_lock();
1381
ecc48a90
JD
1382 /*
1383 * Don't try to enable a channel if the session has been started at
1384 * some point in time before. The tracer does not allow it.
1385 */
8382cf6f 1386 if (session->has_been_started) {
ecc48a90
JD
1387 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1388 goto error;
1389 }
1390
1391 /*
1392 * If the session is a live session, remove the switch timer, the
1393 * live timer does the same thing but sends also synchronisation
1394 * beacons for inactive streams.
1395 */
1396 if (session->live_timer > 0) {
df4f5a87
JG
1397 attr.attr.live_timer_interval = session->live_timer;
1398 attr.attr.switch_timer_interval = 0;
ecc48a90
JD
1399 }
1400
6e21424e
JR
1401 /* Check for feature support */
1402 switch (domain->type) {
1403 case LTTNG_DOMAIN_KERNEL:
1404 {
7d268848 1405 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
6e21424e
JR
1406 /* Sampling position of buffer is not supported */
1407 WARN("Kernel tracer does not support buffer monitoring. "
1408 "Setting the monitor interval timer to 0 "
1409 "(disabled) for channel '%s' of session '%s'",
df4f5a87
JG
1410 attr.name, session->name);
1411 lttng_channel_set_monitor_timer_interval(&attr, 0);
6e21424e
JR
1412 }
1413 break;
1414 }
1415 case LTTNG_DOMAIN_UST:
f28f9e44 1416 break;
6e21424e
JR
1417 case LTTNG_DOMAIN_JUL:
1418 case LTTNG_DOMAIN_LOG4J:
1419 case LTTNG_DOMAIN_PYTHON:
f28f9e44
JG
1420 if (!agent_tracing_is_enabled()) {
1421 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1422 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1423 goto error;
1424 }
6e21424e
JR
1425 break;
1426 default:
1427 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1428 goto error;
1429 }
1430
7972aab2 1431 switch (domain->type) {
2f77fc4b
DG
1432 case LTTNG_DOMAIN_KERNEL:
1433 {
1434 struct ltt_kernel_channel *kchan;
1435
df4f5a87 1436 kchan = trace_kernel_get_channel_by_name(attr.name,
2f77fc4b
DG
1437 session->kernel_session);
1438 if (kchan == NULL) {
54213acc
JG
1439 if (session->snapshot.nb_output > 0 ||
1440 session->snapshot_mode) {
1441 /* Enforce mmap output for snapshot sessions. */
df4f5a87 1442 attr.attr.output = LTTNG_EVENT_MMAP;
54213acc 1443 }
df4f5a87
JG
1444 ret = channel_kernel_create(session->kernel_session, &attr, wpipe);
1445 if (attr.name[0] != '\0') {
85076754
MD
1446 session->kernel_session->has_non_default_channel = 1;
1447 }
2f77fc4b
DG
1448 } else {
1449 ret = channel_kernel_enable(session->kernel_session, kchan);
1450 }
1451
f73fabfd 1452 if (ret != LTTNG_OK) {
2f77fc4b
DG
1453 goto error;
1454 }
1455
7d268848 1456 kernel_wait_quiescent();
2f77fc4b
DG
1457 break;
1458 }
1459 case LTTNG_DOMAIN_UST:
9232818f
JG
1460 case LTTNG_DOMAIN_JUL:
1461 case LTTNG_DOMAIN_LOG4J:
1462 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
1463 {
1464 struct ltt_ust_channel *uchan;
1465
9232818f
JG
1466 /*
1467 * FIXME
1468 *
1469 * Current agent implementation limitations force us to allow
1470 * only one channel at once in "agent" subdomains. Each
1471 * subdomain has a default channel name which must be strictly
1472 * adhered to.
1473 */
1474 if (domain->type == LTTNG_DOMAIN_JUL) {
df4f5a87 1475 if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME,
9232818f
JG
1476 LTTNG_SYMBOL_NAME_LEN)) {
1477 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1478 goto error;
1479 }
1480 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
df4f5a87 1481 if (strncmp(attr.name, DEFAULT_LOG4J_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_PYTHON) {
df4f5a87 1487 if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME,
9232818f
JG
1488 LTTNG_SYMBOL_NAME_LEN)) {
1489 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1490 goto error;
1491 }
1492 }
1493
2f77fc4b
DG
1494 chan_ht = usess->domain_global.channels;
1495
df4f5a87 1496 uchan = trace_ust_find_channel_by_name(chan_ht, attr.name);
2f77fc4b 1497 if (uchan == NULL) {
df4f5a87
JG
1498 ret = channel_ust_create(usess, &attr, domain->buf_type);
1499 if (attr.name[0] != '\0') {
85076754
MD
1500 usess->has_non_default_channel = 1;
1501 }
2f77fc4b 1502 } else {
7972aab2 1503 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1504 }
1505 break;
1506 }
2f77fc4b 1507 default:
f73fabfd 1508 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1509 goto error;
1510 }
1511
df4f5a87 1512 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
54213acc
JG
1513 session->has_non_mmap_channel = true;
1514 }
2f77fc4b 1515error:
2223c96f 1516 rcu_read_unlock();
1f345e94 1517end:
2f77fc4b
DG
1518 return ret;
1519}
1520
159b042f
JG
1521enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1522 struct ltt_session *session,
1523 enum lttng_domain_type domain,
1524 enum lttng_process_attr process_attr,
1525 enum lttng_tracking_policy *policy)
1526{
1527 enum lttng_error_code ret_code = LTTNG_OK;
1528 const struct process_attr_tracker *tracker;
1529
1530 switch (domain) {
1531 case LTTNG_DOMAIN_KERNEL:
1532 if (!session->kernel_session) {
1533 ret_code = LTTNG_ERR_INVALID;
1534 goto end;
1535 }
1536 tracker = kernel_get_process_attr_tracker(
1537 session->kernel_session, process_attr);
1538 break;
1539 case LTTNG_DOMAIN_UST:
1540 if (!session->ust_session) {
1541 ret_code = LTTNG_ERR_INVALID;
1542 goto end;
1543 }
1544 tracker = trace_ust_get_process_attr_tracker(
1545 session->ust_session, process_attr);
1546 break;
1547 default:
1548 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1549 goto end;
1550 }
1551 if (tracker) {
1552 *policy = process_attr_tracker_get_tracking_policy(tracker);
1553 } else {
1554 ret_code = LTTNG_ERR_INVALID;
1555 }
1556end:
1557 return ret_code;
1558}
1559
1560enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1561 struct ltt_session *session,
1562 enum lttng_domain_type domain,
1563 enum lttng_process_attr process_attr,
1564 enum lttng_tracking_policy policy)
1565{
1566 enum lttng_error_code ret_code = LTTNG_OK;
1567
1568 switch (policy) {
1569 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1570 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1571 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1572 break;
1573 default:
1574 ret_code = LTTNG_ERR_INVALID;
1575 goto end;
1576 }
1577
1578 switch (domain) {
1579 case LTTNG_DOMAIN_KERNEL:
1580 if (!session->kernel_session) {
1581 ret_code = LTTNG_ERR_INVALID;
1582 goto end;
1583 }
1584 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1585 session->kernel_session, process_attr, policy);
1586 break;
1587 case LTTNG_DOMAIN_UST:
1588 if (!session->ust_session) {
1589 ret_code = LTTNG_ERR_INVALID;
1590 goto end;
1591 }
1592 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1593 session->ust_session, process_attr, policy);
1594 break;
1595 default:
1596 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1597 break;
1598 }
1599end:
1600 return ret_code;
1601}
1602
1603enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1604 struct ltt_session *session,
1605 enum lttng_domain_type domain,
1606 enum lttng_process_attr process_attr,
1607 const struct process_attr_value *value)
1608{
1609 enum lttng_error_code ret_code = LTTNG_OK;
1610
1611 switch (domain) {
1612 case LTTNG_DOMAIN_KERNEL:
1613 if (!session->kernel_session) {
1614 ret_code = LTTNG_ERR_INVALID;
1615 goto end;
1616 }
1617 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1618 session->kernel_session, process_attr, value);
1619 break;
1620 case LTTNG_DOMAIN_UST:
1621 if (!session->ust_session) {
1622 ret_code = LTTNG_ERR_INVALID;
1623 goto end;
1624 }
1625 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1626 session->ust_session, process_attr, value);
1627 break;
1628 default:
1629 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1630 break;
1631 }
1632end:
1633 return ret_code;
1634}
1635
1636enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1637 struct ltt_session *session,
1638 enum lttng_domain_type domain,
1639 enum lttng_process_attr process_attr,
1640 const struct process_attr_value *value)
1641{
1642 enum lttng_error_code ret_code = LTTNG_OK;
1643
1644 switch (domain) {
1645 case LTTNG_DOMAIN_KERNEL:
1646 if (!session->kernel_session) {
1647 ret_code = LTTNG_ERR_INVALID;
1648 goto end;
1649 }
1650 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1651 session->kernel_session, process_attr, value);
1652 break;
1653 case LTTNG_DOMAIN_UST:
1654 if (!session->ust_session) {
1655 ret_code = LTTNG_ERR_INVALID;
1656 goto end;
1657 }
1658 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1659 session->ust_session, process_attr, value);
1660 break;
1661 default:
1662 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1663 break;
1664 }
1665end:
1666 return ret_code;
1667}
1668
1669enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1670 struct ltt_session *session,
1671 enum lttng_domain_type domain,
1672 enum lttng_process_attr process_attr,
1673 struct lttng_process_attr_values **values)
1674{
1675 enum lttng_error_code ret_code = LTTNG_OK;
1676 const struct process_attr_tracker *tracker;
1677 enum process_attr_tracker_status status;
1678
1679 switch (domain) {
1680 case LTTNG_DOMAIN_KERNEL:
1681 if (!session->kernel_session) {
1682 ret_code = LTTNG_ERR_INVALID;
1683 goto end;
1684 }
1685 tracker = kernel_get_process_attr_tracker(
1686 session->kernel_session, process_attr);
1687 break;
1688 case LTTNG_DOMAIN_UST:
1689 if (!session->ust_session) {
1690 ret_code = LTTNG_ERR_INVALID;
1691 goto end;
1692 }
1693 tracker = trace_ust_get_process_attr_tracker(
1694 session->ust_session, process_attr);
1695 break;
1696 default:
1697 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1698 goto end;
1699 }
1700
1701 if (!tracker) {
1702 ret_code = LTTNG_ERR_INVALID;
1703 goto end;
1704 }
1705
1706 status = process_attr_tracker_get_inclusion_set(tracker, values);
1707 switch (status) {
1708 case PROCESS_ATTR_TRACKER_STATUS_OK:
1709 ret_code = LTTNG_OK;
1710 break;
1711 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1712 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1713 break;
1714 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1715 ret_code = LTTNG_ERR_NOMEM;
1716 break;
1717 default:
1718 ret_code = LTTNG_ERR_UNK;
1719 break;
1720 }
1721
1722end:
1723 return ret_code;
1724}
1725
2f77fc4b
DG
1726/*
1727 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1728 */
56a37563 1729int cmd_disable_event(struct ltt_session *session,
df4f5a87
JG
1730 enum lttng_domain_type domain, const char *channel_name,
1731 const struct lttng_event *event)
2f77fc4b
DG
1732{
1733 int ret;
df4f5a87 1734 const char *event_name;
6e911cad 1735
18a720cd
MD
1736 DBG("Disable event command for event \'%s\'", event->name);
1737
6e911cad
MD
1738 event_name = event->name;
1739
9b7431cf
JG
1740 /* Error out on unhandled search criteria */
1741 if (event->loglevel_type || event->loglevel != -1 || event->enabled
6e911cad 1742 || event->pid || event->filter || event->exclusion) {
7076b56e
JG
1743 ret = LTTNG_ERR_UNK;
1744 goto error;
6e911cad 1745 }
2f77fc4b 1746
2223c96f
DG
1747 rcu_read_lock();
1748
2f77fc4b
DG
1749 switch (domain) {
1750 case LTTNG_DOMAIN_KERNEL:
1751 {
1752 struct ltt_kernel_channel *kchan;
1753 struct ltt_kernel_session *ksess;
1754
1755 ksess = session->kernel_session;
1756
85076754
MD
1757 /*
1758 * If a non-default channel has been created in the
1759 * session, explicitely require that -c chan_name needs
1760 * to be provided.
1761 */
1762 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1763 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1764 goto error_unlock;
85076754
MD
1765 }
1766
2f77fc4b
DG
1767 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1768 if (kchan == NULL) {
f73fabfd 1769 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
7076b56e 1770 goto error_unlock;
2f77fc4b
DG
1771 }
1772
6e911cad
MD
1773 switch (event->type) {
1774 case LTTNG_EVENT_ALL:
9550ee81 1775 case LTTNG_EVENT_TRACEPOINT:
d0ae4ea8 1776 case LTTNG_EVENT_SYSCALL:
9550ee81
JR
1777 case LTTNG_EVENT_PROBE:
1778 case LTTNG_EVENT_FUNCTION:
1779 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1780 if (event_name[0] == '\0') {
1781 ret = event_kernel_disable_event(kchan,
1782 NULL, event->type);
29c62722 1783 } else {
d0ae4ea8 1784 ret = event_kernel_disable_event(kchan,
9550ee81 1785 event_name, event->type);
29c62722 1786 }
6e911cad 1787 if (ret != LTTNG_OK) {
7076b56e 1788 goto error_unlock;
6e911cad
MD
1789 }
1790 break;
6e911cad
MD
1791 default:
1792 ret = LTTNG_ERR_UNK;
7076b56e 1793 goto error_unlock;
2f77fc4b
DG
1794 }
1795
7d268848 1796 kernel_wait_quiescent();
2f77fc4b
DG
1797 break;
1798 }
1799 case LTTNG_DOMAIN_UST:
1800 {
1801 struct ltt_ust_channel *uchan;
1802 struct ltt_ust_session *usess;
1803
1804 usess = session->ust_session;
1805
7076b56e
JG
1806 if (validate_ust_event_name(event_name)) {
1807 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1808 goto error_unlock;
1809 }
1810
85076754
MD
1811 /*
1812 * If a non-default channel has been created in the
9550ee81 1813 * session, explicitly require that -c chan_name needs
85076754
MD
1814 * to be provided.
1815 */
1816 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1817 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1818 goto error_unlock;
85076754
MD
1819 }
1820
2f77fc4b
DG
1821 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1822 channel_name);
1823 if (uchan == NULL) {
f73fabfd 1824 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
7076b56e 1825 goto error_unlock;
2f77fc4b
DG
1826 }
1827
6e911cad
MD
1828 switch (event->type) {
1829 case LTTNG_EVENT_ALL:
b3639870
JR
1830 /*
1831 * An empty event name means that everything
1832 * should be disabled.
1833 */
1834 if (event->name[0] == '\0') {
1835 ret = event_ust_disable_all_tracepoints(usess, uchan);
77d536b2
JR
1836 } else {
1837 ret = event_ust_disable_tracepoint(usess, uchan,
1838 event_name);
1839 }
6e911cad 1840 if (ret != LTTNG_OK) {
7076b56e 1841 goto error_unlock;
6e911cad
MD
1842 }
1843 break;
1844 default:
1845 ret = LTTNG_ERR_UNK;
7076b56e 1846 goto error_unlock;
2f77fc4b
DG
1847 }
1848
1849 DBG3("Disable UST event %s in channel %s completed", event_name,
1850 channel_name);
1851 break;
1852 }
5cdb6027 1853 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1854 case LTTNG_DOMAIN_JUL:
0e115563 1855 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1856 {
fefd409b 1857 struct agent *agt;
f20baf8e
DG
1858 struct ltt_ust_session *usess = session->ust_session;
1859
1860 assert(usess);
1861
6e911cad
MD
1862 switch (event->type) {
1863 case LTTNG_EVENT_ALL:
1864 break;
1865 default:
1866 ret = LTTNG_ERR_UNK;
7076b56e 1867 goto error_unlock;
6e911cad
MD
1868 }
1869
5cdb6027 1870 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1871 if (!agt) {
1872 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
7076b56e 1873 goto error_unlock;
fefd409b 1874 }
b3639870
JR
1875 /*
1876 * An empty event name means that everything
1877 * should be disabled.
1878 */
1879 if (event->name[0] == '\0') {
18a720cd
MD
1880 ret = event_agent_disable_all(usess, agt);
1881 } else {
1882 ret = event_agent_disable(usess, agt, event_name);
1883 }
f20baf8e 1884 if (ret != LTTNG_OK) {
7076b56e 1885 goto error_unlock;
f20baf8e
DG
1886 }
1887
1888 break;
1889 }
2f77fc4b 1890 default:
f73fabfd 1891 ret = LTTNG_ERR_UND;
7076b56e 1892 goto error_unlock;
2f77fc4b
DG
1893 }
1894
f73fabfd 1895 ret = LTTNG_OK;
2f77fc4b 1896
7076b56e 1897error_unlock:
2223c96f 1898 rcu_read_unlock();
7076b56e 1899error:
2f77fc4b
DG
1900 return ret;
1901}
1902
2f77fc4b
DG
1903/*
1904 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1905 */
56a37563 1906int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
df4f5a87 1907 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1908{
d5979e4a 1909 int ret, chan_kern_created = 0, chan_ust_created = 0;
bdf64013
JG
1910 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1911
9a699f7b
JR
1912 /*
1913 * Don't try to add a context if the session has been started at
1914 * some point in time before. The tracer does not allow it and would
1915 * result in a corrupted trace.
1916 */
1917 if (session->has_been_started) {
1918 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1919 goto end;
1920 }
1921
bdf64013
JG
1922 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1923 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1924 app_ctx_name = ctx->u.app_ctx.ctx_name;
1925 }
2f77fc4b
DG
1926
1927 switch (domain) {
1928 case LTTNG_DOMAIN_KERNEL:
979e618e
DG
1929 assert(session->kernel_session);
1930
1931 if (session->kernel_session->channel_count == 0) {
1932 /* Create default channel */
1933 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1934 if (ret != LTTNG_OK) {
1935 goto error;
1936 }
d5979e4a 1937 chan_kern_created = 1;
979e618e 1938 }
2f77fc4b 1939 /* Add kernel context to kernel tracer */
601d5acf 1940 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 1941 if (ret != LTTNG_OK) {
2f77fc4b
DG
1942 goto error;
1943 }
1944 break;
bdf64013
JG
1945 case LTTNG_DOMAIN_JUL:
1946 case LTTNG_DOMAIN_LOG4J:
1947 {
1948 /*
1949 * Validate channel name.
1950 * If no channel name is given and the domain is JUL or LOG4J,
1951 * set it to the appropriate domain-specific channel name. If
1952 * a name is provided but does not match the expexted channel
1953 * name, return an error.
1954 */
1955 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1956 strcmp(channel_name,
1957 DEFAULT_JUL_CHANNEL_NAME)) {
1958 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1959 goto error;
1960 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1961 strcmp(channel_name,
1962 DEFAULT_LOG4J_CHANNEL_NAME)) {
1963 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1964 goto error;
1965 }
a93b3916 1966 /* break is _not_ missing here. */
bdf64013 1967 }
2f77fc4b
DG
1968 case LTTNG_DOMAIN_UST:
1969 {
1970 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
1971 unsigned int chan_count;
1972
2f77fc4b
DG
1973 assert(usess);
1974
85076754 1975 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
1976 if (chan_count == 0) {
1977 struct lttng_channel *attr;
1978 /* Create default channel */
0a9c6494 1979 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
1980 if (attr == NULL) {
1981 ret = LTTNG_ERR_FATAL;
1982 goto error;
1983 }
1984
7972aab2 1985 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
1986 if (ret != LTTNG_OK) {
1987 free(attr);
1988 goto error;
1989 }
cf0bcb51 1990 channel_attr_destroy(attr);
d5979e4a 1991 chan_ust_created = 1;
979e618e
DG
1992 }
1993
601d5acf 1994 ret = context_ust_add(usess, domain, ctx, channel_name);
bdf64013
JG
1995 free(app_ctx_provider_name);
1996 free(app_ctx_name);
1997 app_ctx_name = NULL;
1998 app_ctx_provider_name = NULL;
f73fabfd 1999 if (ret != LTTNG_OK) {
2f77fc4b
DG
2000 goto error;
2001 }
2002 break;
2003 }
2f77fc4b 2004 default:
f73fabfd 2005 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2006 goto error;
2007 }
2008
bdf64013
JG
2009 ret = LTTNG_OK;
2010 goto end;
2f77fc4b
DG
2011
2012error:
d5979e4a
DG
2013 if (chan_kern_created) {
2014 struct ltt_kernel_channel *kchan =
2015 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2016 session->kernel_session);
2017 /* Created previously, this should NOT fail. */
2018 assert(kchan);
2019 kernel_destroy_channel(kchan);
2020 }
2021
2022 if (chan_ust_created) {
2023 struct ltt_ust_channel *uchan =
2024 trace_ust_find_channel_by_name(
2025 session->ust_session->domain_global.channels,
2026 DEFAULT_CHANNEL_NAME);
2027 /* Created previously, this should NOT fail. */
2028 assert(uchan);
2029 /* Remove from the channel list of the session. */
2030 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2031 uchan);
2032 trace_ust_destroy_channel(uchan);
2033 }
bdf64013
JG
2034end:
2035 free(app_ctx_provider_name);
2036 free(app_ctx_name);
2f77fc4b
DG
2037 return ret;
2038}
2039
dac8e046
JG
2040static inline bool name_starts_with(const char *name, const char *prefix)
2041{
2042 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2043
2044 return !strncmp(name, prefix, max_cmp_len);
2045}
2046
2047/* Perform userspace-specific event name validation */
2048static int validate_ust_event_name(const char *name)
2049{
2050 int ret = 0;
2051
2052 if (!name) {
2053 ret = -1;
2054 goto end;
2055 }
2056
2057 /*
2058 * Check name against all internal UST event component namespaces used
2059 * by the agents.
2060 */
2061 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2062 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2063 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2064 ret = -1;
2065 }
2066
2067end:
2068 return ret;
2069}
88f06f15 2070
2f77fc4b 2071/*
88f06f15
JG
2072 * Internal version of cmd_enable_event() with a supplemental
2073 * "internal_event" flag which is used to enable internal events which should
2074 * be hidden from clients. Such events are used in the agent implementation to
2075 * enable the events through which all "agent" events are funeled.
2f77fc4b 2076 */
88f06f15 2077static int _cmd_enable_event(struct ltt_session *session,
df4f5a87 2078 const struct lttng_domain *domain,
025faf73 2079 char *channel_name, struct lttng_event *event,
6b453b5e 2080 char *filter_expression,
db8f1377
JI
2081 struct lttng_filter_bytecode *filter,
2082 struct lttng_event_exclusion *exclusion,
88f06f15 2083 int wpipe, bool internal_event)
2f77fc4b 2084{
9f449915 2085 int ret = 0, channel_created = 0;
cfedea03 2086 struct lttng_channel *attr = NULL;
2f77fc4b
DG
2087
2088 assert(session);
2089 assert(event);
2090 assert(channel_name);
2091
2a385866
JG
2092 /* If we have a filter, we must have its filter expression */
2093 assert(!(!!filter_expression ^ !!filter));
2094
9f449915
PP
2095 /* Normalize event name as a globbing pattern */
2096 strutils_normalize_star_glob_pattern(event->name);
18a720cd 2097
9f449915
PP
2098 /* Normalize exclusion names as globbing patterns */
2099 if (exclusion) {
2100 size_t i;
f5ac4bd7 2101
9f449915
PP
2102 for (i = 0; i < exclusion->count; i++) {
2103 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2104
2105 strutils_normalize_star_glob_pattern(name);
2106 }
930a2e99
JG
2107 }
2108
9f449915
PP
2109 DBG("Enable event command for event \'%s\'", event->name);
2110
2111 rcu_read_lock();
2112
7972aab2 2113 switch (domain->type) {
2f77fc4b
DG
2114 case LTTNG_DOMAIN_KERNEL:
2115 {
2116 struct ltt_kernel_channel *kchan;
2117
85076754
MD
2118 /*
2119 * If a non-default channel has been created in the
2120 * session, explicitely require that -c chan_name needs
2121 * to be provided.
2122 */
2123 if (session->kernel_session->has_non_default_channel
2124 && channel_name[0] == '\0') {
2125 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2126 goto error;
2127 }
2128
2f77fc4b
DG
2129 kchan = trace_kernel_get_channel_by_name(channel_name,
2130 session->kernel_session);
2131 if (kchan == NULL) {
0a9c6494
DG
2132 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2133 LTTNG_BUFFER_GLOBAL);
2f77fc4b 2134 if (attr == NULL) {
f73fabfd 2135 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2136 goto error;
2137 }
04c17253
MD
2138 if (lttng_strncpy(attr->name, channel_name,
2139 sizeof(attr->name))) {
2140 ret = LTTNG_ERR_INVALID;
04c17253
MD
2141 goto error;
2142 }
2f77fc4b
DG
2143
2144 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 2145 if (ret != LTTNG_OK) {
2f77fc4b
DG
2146 goto error;
2147 }
e5f5db7f 2148 channel_created = 1;
2f77fc4b
DG
2149 }
2150
2151 /* Get the newly created kernel channel pointer */
2152 kchan = trace_kernel_get_channel_by_name(channel_name,
2153 session->kernel_session);
2154 if (kchan == NULL) {
2155 /* This sould not happen... */
f73fabfd 2156 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2157 goto error;
2158 }
2159
6e911cad
MD
2160 switch (event->type) {
2161 case LTTNG_EVENT_ALL:
29c62722 2162 {
00a62084
MD
2163 char *filter_expression_a = NULL;
2164 struct lttng_filter_bytecode *filter_a = NULL;
2165
2166 /*
2167 * We need to duplicate filter_expression and filter,
2168 * because ownership is passed to first enable
2169 * event.
2170 */
2171 if (filter_expression) {
2172 filter_expression_a = strdup(filter_expression);
2173 if (!filter_expression_a) {
2174 ret = LTTNG_ERR_FATAL;
2175 goto error;
2176 }
2177 }
2178 if (filter) {
2179 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2180 if (!filter_a) {
2181 free(filter_expression_a);
2182 ret = LTTNG_ERR_FATAL;
2183 goto error;
2184 }
2185 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2186 }
29c62722 2187 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
00a62084
MD
2188 ret = event_kernel_enable_event(kchan, event,
2189 filter_expression, filter);
a969e101
MD
2190 /* We have passed ownership */
2191 filter_expression = NULL;
2192 filter = NULL;
29c62722
MD
2193 if (ret != LTTNG_OK) {
2194 if (channel_created) {
2195 /* Let's not leak a useless channel. */
2196 kernel_destroy_channel(kchan);
2197 }
00a62084
MD
2198 free(filter_expression_a);
2199 free(filter_a);
29c62722
MD
2200 goto error;
2201 }
2202 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
00a62084
MD
2203 ret = event_kernel_enable_event(kchan, event,
2204 filter_expression_a, filter_a);
60d21fa2
AB
2205 /* We have passed ownership */
2206 filter_expression_a = NULL;
2207 filter_a = NULL;
29c62722
MD
2208 if (ret != LTTNG_OK) {
2209 goto error;
2210 }
2211 break;
2212 }
6e6ef3d7 2213 case LTTNG_EVENT_PROBE:
dcabc190 2214 case LTTNG_EVENT_USERSPACE_PROBE:
6e6ef3d7
DG
2215 case LTTNG_EVENT_FUNCTION:
2216 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 2217 case LTTNG_EVENT_TRACEPOINT:
00a62084
MD
2218 ret = event_kernel_enable_event(kchan, event,
2219 filter_expression, filter);
a969e101
MD
2220 /* We have passed ownership */
2221 filter_expression = NULL;
2222 filter = NULL;
6e911cad
MD
2223 if (ret != LTTNG_OK) {
2224 if (channel_created) {
2225 /* Let's not leak a useless channel. */
2226 kernel_destroy_channel(kchan);
2227 }
2228 goto error;
e5f5db7f 2229 }
6e911cad
MD
2230 break;
2231 case LTTNG_EVENT_SYSCALL:
00a62084
MD
2232 ret = event_kernel_enable_event(kchan, event,
2233 filter_expression, filter);
a969e101
MD
2234 /* We have passed ownership */
2235 filter_expression = NULL;
2236 filter = NULL;
e2b957af
MD
2237 if (ret != LTTNG_OK) {
2238 goto error;
2239 }
6e911cad
MD
2240 break;
2241 default:
2242 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
2243 goto error;
2244 }
2245
7d268848 2246 kernel_wait_quiescent();
2f77fc4b
DG
2247 break;
2248 }
2249 case LTTNG_DOMAIN_UST:
2250 {
2251 struct ltt_ust_channel *uchan;
2252 struct ltt_ust_session *usess = session->ust_session;
2253
2254 assert(usess);
2255
85076754
MD
2256 /*
2257 * If a non-default channel has been created in the
2258 * session, explicitely require that -c chan_name needs
2259 * to be provided.
2260 */
2261 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2262 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2263 goto error;
2264 }
2265
2f77fc4b
DG
2266 /* Get channel from global UST domain */
2267 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2268 channel_name);
2269 if (uchan == NULL) {
2270 /* Create default channel */
0a9c6494
DG
2271 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2272 usess->buffer_type);
2f77fc4b 2273 if (attr == NULL) {
f73fabfd 2274 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2275 goto error;
2276 }
04c17253
MD
2277 if (lttng_strncpy(attr->name, channel_name,
2278 sizeof(attr->name))) {
2279 ret = LTTNG_ERR_INVALID;
04c17253
MD
2280 goto error;
2281 }
2f77fc4b
DG
2282
2283 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 2284 if (ret != LTTNG_OK) {
2f77fc4b
DG
2285 goto error;
2286 }
2f77fc4b
DG
2287
2288 /* Get the newly created channel reference back */
2289 uchan = trace_ust_find_channel_by_name(
2290 usess->domain_global.channels, channel_name);
2291 assert(uchan);
2292 }
2293
141feb8c
JG
2294 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2295 /*
2296 * Don't allow users to add UST events to channels which
2297 * are assigned to a userspace subdomain (JUL, Log4J,
2298 * Python, etc.).
2299 */
2300 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2301 goto error;
2302 }
2303
dac8e046
JG
2304 if (!internal_event) {
2305 /*
2306 * Ensure the event name is not reserved for internal
2307 * use.
2308 */
2309 ret = validate_ust_event_name(event->name);
2310 if (ret) {
0e270a1e 2311 WARN("Userspace event name %s failed validation.",
bbcab087 2312 event->name);
dac8e046
JG
2313 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2314 goto error;
2315 }
2316 }
2317
2f77fc4b 2318 /* At this point, the session and channel exist on the tracer */
6b453b5e 2319 ret = event_ust_enable_tracepoint(usess, uchan, event,
88f06f15
JG
2320 filter_expression, filter, exclusion,
2321 internal_event);
49d21f93
MD
2322 /* We have passed ownership */
2323 filter_expression = NULL;
2324 filter = NULL;
2325 exclusion = NULL;
94382e15
JG
2326 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2327 goto already_enabled;
2328 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2329 goto error;
2330 }
2331 break;
2332 }
5cdb6027 2333 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2334 case LTTNG_DOMAIN_JUL:
0e115563 2335 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2336 {
da6c3a50 2337 const char *default_event_name, *default_chan_name;
fefd409b 2338 struct agent *agt;
f20baf8e
DG
2339 struct lttng_event uevent;
2340 struct lttng_domain tmp_dom;
2341 struct ltt_ust_session *usess = session->ust_session;
2342
2343 assert(usess);
2344
f28f9e44
JG
2345 if (!agent_tracing_is_enabled()) {
2346 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2347 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2348 goto error;
2349 }
2350
5cdb6027 2351 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2352 if (!agt) {
5cdb6027 2353 agt = agent_create(domain->type);
fefd409b 2354 if (!agt) {
e5b3c48c 2355 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2356 goto error;
2357 }
2358 agent_add(agt, usess->agents);
2359 }
2360
022d91ba 2361 /* Create the default tracepoint. */
996de3c7 2362 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2363 uevent.type = LTTNG_EVENT_TRACEPOINT;
2364 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
51755dc8
JG
2365 default_event_name = event_get_default_agent_ust_name(
2366 domain->type);
da6c3a50 2367 if (!default_event_name) {
e5b3c48c 2368 ret = LTTNG_ERR_FATAL;
da6c3a50 2369 goto error;
f43f95a9 2370 }
da6c3a50 2371 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2372 uevent.name[sizeof(uevent.name) - 1] = '\0';
2373
2374 /*
2375 * The domain type is changed because we are about to enable the
2376 * default channel and event for the JUL domain that are hardcoded.
2377 * This happens in the UST domain.
2378 */
2379 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2380 tmp_dom.type = LTTNG_DOMAIN_UST;
2381
0e115563
DG
2382 switch (domain->type) {
2383 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2384 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2385 break;
2386 case LTTNG_DOMAIN_JUL:
da6c3a50 2387 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2388 break;
2389 case LTTNG_DOMAIN_PYTHON:
2390 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2391 break;
2392 default:
e98a44b0 2393 /* The switch/case we are in makes this impossible */
0e115563 2394 assert(0);
da6c3a50
DG
2395 }
2396
971da06a 2397 {
8404118c 2398 char *filter_expression_copy = NULL;
51755dc8 2399 struct lttng_filter_bytecode *filter_copy = NULL;
971da06a
JG
2400
2401 if (filter) {
51755dc8
JG
2402 const size_t filter_size = sizeof(
2403 struct lttng_filter_bytecode)
2404 + filter->len;
2405
2406 filter_copy = zmalloc(filter_size);
971da06a 2407 if (!filter_copy) {
018096a4 2408 ret = LTTNG_ERR_NOMEM;
b742e3e2 2409 goto error;
971da06a 2410 }
51755dc8 2411 memcpy(filter_copy, filter, filter_size);
971da06a 2412
8404118c
JG
2413 filter_expression_copy =
2414 strdup(filter_expression);
2415 if (!filter_expression) {
2416 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2417 }
2418
2419 if (!filter_expression_copy || !filter_copy) {
2420 free(filter_expression_copy);
2421 free(filter_copy);
2422 goto error;
8404118c 2423 }
971da06a
JG
2424 }
2425
88f06f15 2426 ret = cmd_enable_event_internal(session, &tmp_dom,
971da06a 2427 (char *) default_chan_name,
8404118c
JG
2428 &uevent, filter_expression_copy,
2429 filter_copy, NULL, wpipe);
971da06a
JG
2430 }
2431
94382e15
JG
2432 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2433 goto already_enabled;
2434 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2435 goto error;
2436 }
2437
2438 /* The wild card * means that everything should be enabled. */
2439 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
8404118c
JG
2440 ret = event_agent_enable_all(usess, agt, event, filter,
2441 filter_expression);
f20baf8e 2442 } else {
8404118c
JG
2443 ret = event_agent_enable(usess, agt, event, filter,
2444 filter_expression);
f20baf8e 2445 }
51755dc8
JG
2446 filter = NULL;
2447 filter_expression = NULL;
f20baf8e
DG
2448 if (ret != LTTNG_OK) {
2449 goto error;
2450 }
2451
2452 break;
2453 }
2f77fc4b 2454 default:
f73fabfd 2455 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2456 goto error;
2457 }
2458
f73fabfd 2459 ret = LTTNG_OK;
2f77fc4b 2460
94382e15 2461already_enabled:
2f77fc4b 2462error:
49d21f93
MD
2463 free(filter_expression);
2464 free(filter);
2465 free(exclusion);
cf0bcb51 2466 channel_attr_destroy(attr);
2223c96f 2467 rcu_read_unlock();
2f77fc4b
DG
2468 return ret;
2469}
2470
88f06f15
JG
2471/*
2472 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2473 * We own filter, exclusion, and filter_expression.
2474 */
df4f5a87
JG
2475int cmd_enable_event(struct ltt_session *session,
2476 const struct lttng_domain *domain,
88f06f15
JG
2477 char *channel_name, struct lttng_event *event,
2478 char *filter_expression,
2479 struct lttng_filter_bytecode *filter,
2480 struct lttng_event_exclusion *exclusion,
2481 int wpipe)
2482{
2483 return _cmd_enable_event(session, domain, channel_name, event,
2484 filter_expression, filter, exclusion, wpipe, false);
2485}
2486
2487/*
2488 * Enable an event which is internal to LTTng. An internal should
2489 * never be made visible to clients and are immune to checks such as
2490 * reserved names.
2491 */
2492static int cmd_enable_event_internal(struct ltt_session *session,
df4f5a87 2493 const struct lttng_domain *domain,
88f06f15
JG
2494 char *channel_name, struct lttng_event *event,
2495 char *filter_expression,
2496 struct lttng_filter_bytecode *filter,
2497 struct lttng_event_exclusion *exclusion,
2498 int wpipe)
2499{
2500 return _cmd_enable_event(session, domain, channel_name, event,
2501 filter_expression, filter, exclusion, wpipe, true);
2502}
2503
2f77fc4b
DG
2504/*
2505 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2506 */
56a37563
JG
2507ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2508 struct lttng_event **events)
2f77fc4b
DG
2509{
2510 int ret;
2511 ssize_t nb_events = 0;
2512
2513 switch (domain) {
2514 case LTTNG_DOMAIN_KERNEL:
7d268848 2515 nb_events = kernel_list_events(events);
2f77fc4b 2516 if (nb_events < 0) {
f73fabfd 2517 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
2518 goto error;
2519 }
2520 break;
2521 case LTTNG_DOMAIN_UST:
2522 nb_events = ust_app_list_events(events);
2523 if (nb_events < 0) {
f73fabfd 2524 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2525 goto error;
2526 }
2527 break;
5cdb6027 2528 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2529 case LTTNG_DOMAIN_JUL:
0e115563 2530 case LTTNG_DOMAIN_PYTHON:
f60140a1 2531 nb_events = agent_list_events(events, domain);
3c6a091f
DG
2532 if (nb_events < 0) {
2533 ret = LTTNG_ERR_UST_LIST_FAIL;
2534 goto error;
2535 }
2536 break;
2f77fc4b 2537 default:
f73fabfd 2538 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2539 goto error;
2540 }
2541
2542 return nb_events;
2543
2544error:
2545 /* Return negative value to differentiate return code */
2546 return -ret;
2547}
2548
2549/*
2550 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2551 */
56a37563 2552ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2f77fc4b
DG
2553 struct lttng_event_field **fields)
2554{
2555 int ret;
2556 ssize_t nb_fields = 0;
2557
2558 switch (domain) {
2559 case LTTNG_DOMAIN_UST:
2560 nb_fields = ust_app_list_event_fields(fields);
2561 if (nb_fields < 0) {
f73fabfd 2562 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2563 goto error;
2564 }
2565 break;
2566 case LTTNG_DOMAIN_KERNEL:
2567 default: /* fall-through */
f73fabfd 2568 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2569 goto error;
2570 }
2571
2572 return nb_fields;
2573
2574error:
2575 /* Return negative value to differentiate return code */
2576 return -ret;
2577}
2578
834978fd
DG
2579ssize_t cmd_list_syscalls(struct lttng_event **events)
2580{
2581 return syscall_table_list(events);
2582}
2583
2f77fc4b
DG
2584/*
2585 * Command LTTNG_START_TRACE processed by the client thread.
a9ad0c8f
MD
2586 *
2587 * Called with session mutex held.
2f77fc4b
DG
2588 */
2589int cmd_start_trace(struct ltt_session *session)
2590{
82b69413 2591 enum lttng_error_code ret;
cde3e505 2592 unsigned long nb_chan = 0;
2f77fc4b
DG
2593 struct ltt_kernel_session *ksession;
2594 struct ltt_ust_session *usess;
1f496244
JG
2595 const bool session_rotated_after_last_stop =
2596 session->rotated_after_last_stop;
b02f5986
MD
2597 const bool session_cleared_after_last_stop =
2598 session->cleared_after_last_stop;
2f77fc4b
DG
2599
2600 assert(session);
2601
2602 /* Ease our life a bit ;) */
2603 ksession = session->kernel_session;
2604 usess = session->ust_session;
2605
8382cf6f
DG
2606 /* Is the session already started? */
2607 if (session->active) {
f73fabfd 2608 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
7a24ece3
JR
2609 /* Perform nothing */
2610 goto end;
2f77fc4b
DG
2611 }
2612
1f496244
JG
2613 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2614 !session->current_trace_chunk) {
2615 /*
2616 * A rotation was launched while the session was stopped and
2617 * it has not been completed yet. It is not possible to start
2618 * the session since starting the session here would require a
2619 * rotation from "NULL" to a new trace chunk. That rotation
2620 * would overlap with the ongoing rotation, which is not
2621 * supported.
2622 */
2623 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2624 session->name);
2625 ret = LTTNG_ERR_ROTATION_PENDING;
2626 goto error;
2627 }
2628
cde3e505
DG
2629 /*
2630 * Starting a session without channel is useless since after that it's not
2631 * possible to enable channel thus inform the client.
2632 */
2633 if (usess && usess->domain_global.channels) {
2634 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2635 }
2636 if (ksession) {
2637 nb_chan += ksession->channel_count;
2638 }
2639 if (!nb_chan) {
2640 ret = LTTNG_ERR_NO_CHANNEL;
2641 goto error;
2642 }
2643
1f496244
JG
2644 session->active = 1;
2645 session->rotated_after_last_stop = false;
b02f5986 2646 session->cleared_after_last_stop = false;
070b6a86 2647 if (session->output_traces && !session->current_trace_chunk) {
1f496244
JG
2648 if (!session->has_been_started) {
2649 struct lttng_trace_chunk *trace_chunk;
2650
2651 DBG("Creating initial trace chunk of session \"%s\"",
2652 session->name);
2653 trace_chunk = session_create_new_trace_chunk(
2654 session, NULL, NULL, NULL);
2655 if (!trace_chunk) {
2656 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2657 goto error;
2658 }
2659 assert(!session->current_trace_chunk);
2660 ret = session_set_trace_chunk(session, trace_chunk,
2661 NULL);
2662 lttng_trace_chunk_put(trace_chunk);
2663 if (ret) {
2664 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2665 goto error;
2666 }
2667 } else {
2668 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2669 session->name);
2670 /*
2671 * Rotate existing streams into the new chunk.
2672 * This is a "quiet" rotation has no client has
2673 * explicitly requested this operation.
2674 *
2675 * There is also no need to wait for the rotation
2676 * to complete as it will happen immediately. No data
2677 * was produced as the session was stopped, so the
2678 * rotation should happen on reception of the command.
2679 */
343defc2
MD
2680 ret = cmd_rotate_session(session, NULL, true,
2681 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
1f496244
JG
2682 if (ret != LTTNG_OK) {
2683 goto error;
2684 }
5c408ad8 2685 }
c996624c
JD
2686 }
2687
2f77fc4b
DG
2688 /* Kernel tracing */
2689 if (ksession != NULL) {
c996624c 2690 DBG("Start kernel tracing session %s", session->name);
7d268848 2691 ret = start_kernel_session(ksession);
9b6c7ec5 2692 if (ret != LTTNG_OK) {
2f77fc4b
DG
2693 goto error;
2694 }
2f77fc4b
DG
2695 }
2696
2697 /* Flag session that trace should start automatically */
2698 if (usess) {
82b69413
JG
2699 int int_ret = ust_app_start_trace_all(usess);
2700
2701 if (int_ret < 0) {
f73fabfd 2702 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
2703 goto error;
2704 }
2705 }
2706
04ed9e10
JG
2707 /*
2708 * Open a packet in every stream of the session to ensure that viewers
2709 * can correctly identify the boundaries of the periods during which
2710 * tracing was active for this session.
2711 */
2712 ret = session_open_packets(session);
2713 if (ret != LTTNG_OK) {
2714 goto error;
2715 }
2716
5c408ad8
JD
2717 /*
2718 * Clear the flag that indicates that a rotation was done while the
2719 * session was stopped.
2720 */
2721 session->rotated_after_last_stop = false;
2722
259c2674 2723 if (session->rotate_timer_period) {
82b69413
JG
2724 int int_ret = timer_session_rotation_schedule_timer_start(
2725 session, session->rotate_timer_period);
2726
2727 if (int_ret < 0) {
259c2674
JD
2728 ERR("Failed to enable rotate timer");
2729 ret = LTTNG_ERR_UNK;
2730 goto error;
2731 }
2732 }
2733
f73fabfd 2734 ret = LTTNG_OK;
2f77fc4b
DG
2735
2736error:
1f496244
JG
2737 if (ret == LTTNG_OK) {
2738 /* Flag this after a successful start. */
2739 session->has_been_started |= 1;
2740 } else {
2741 session->active = 0;
2742 /* Restore initial state on error. */
2743 session->rotated_after_last_stop =
2744 session_rotated_after_last_stop;
b02f5986
MD
2745 session->cleared_after_last_stop =
2746 session_cleared_after_last_stop;
1f496244 2747 }
7a24ece3 2748end:
2f77fc4b
DG
2749 return ret;
2750}
2751
2752/*
2753 * Command LTTNG_STOP_TRACE processed by the client thread.
2754 */
2755int cmd_stop_trace(struct ltt_session *session)
2756{
2757 int ret;
2f77fc4b
DG
2758 struct ltt_kernel_session *ksession;
2759 struct ltt_ust_session *usess;
2760
2761 assert(session);
2762
4dbe1875 2763 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2f77fc4b
DG
2764 /* Short cut */
2765 ksession = session->kernel_session;
2766 usess = session->ust_session;
2767
8382cf6f
DG
2768 /* Session is not active. Skip everythong and inform the client. */
2769 if (!session->active) {
f73fabfd 2770 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
2771 goto error;
2772 }
2773
4dbe1875
MD
2774 ret = stop_kernel_session(ksession);
2775 if (ret != LTTNG_OK) {
2776 goto error;
2f77fc4b
DG
2777 }
2778
14fb1ebe 2779 if (usess && usess->active) {
2f77fc4b
DG
2780 ret = ust_app_stop_trace_all(usess);
2781 if (ret < 0) {
f73fabfd 2782 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2783 goto error;
2784 }
2785 }
2786
4dbe1875
MD
2787 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2788 session->id);
8382cf6f
DG
2789 /* Flag inactive after a successful stop. */
2790 session->active = 0;
4dbe1875 2791 ret = LTTNG_OK;
2f77fc4b
DG
2792
2793error:
2794 return ret;
2795}
2796
2797/*
433f5ba9
JR
2798 * Set the base_path of the session only if subdir of a control uris is set.
2799 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2f77fc4b 2800 */
433f5ba9
JR
2801static int set_session_base_path_from_uris(struct ltt_session *session,
2802 size_t nb_uri,
bda32d56 2803 struct lttng_uri *uris)
2f77fc4b 2804{
433f5ba9
JR
2805 int ret;
2806 size_t i;
2f77fc4b 2807
e3876bf0
JR
2808 for (i = 0; i < nb_uri; i++) {
2809 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2810 uris[i].subdir[0] == '\0') {
2811 /* Not interested in these URIs */
2812 continue;
2813 }
2814
2815 if (session->base_path != NULL) {
2816 free(session->base_path);
2817 session->base_path = NULL;
2818 }
2819
2820 /* Set session base_path */
2821 session->base_path = strdup(uris[i].subdir);
2822 if (!session->base_path) {
433f5ba9
JR
2823 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2824 uris[i].subdir, session->name);
2825 ret = LTTNG_ERR_NOMEM;
e3876bf0
JR
2826 goto error;
2827 }
433f5ba9
JR
2828 DBG2("Setting base path \"%s\" for session \"%s\"",
2829 session->base_path, session->name);
2830 }
2831 ret = LTTNG_OK;
2832error:
2833 return ret;
2834}
2835
2836/*
2837 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2838 */
2839int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2840 struct lttng_uri *uris)
2841{
2842 int ret, i;
2843 struct ltt_kernel_session *ksess = session->kernel_session;
2844 struct ltt_ust_session *usess = session->ust_session;
2845
2846 assert(session);
2847 assert(uris);
2848 assert(nb_uri > 0);
2849
2850 /* Can't set consumer URI if the session is active. */
2851 if (session->active) {
2852 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2853 goto error;
2854 }
2855
2856 /*
2857 * Set the session base path if any. This is done inside
2858 * cmd_set_consumer_uri to preserve backward compatibility of the
2859 * previous session creation api vs the session descriptor api.
2860 */
2861 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2862 if (ret != LTTNG_OK) {
2863 goto error;
e3876bf0
JR
2864 }
2865
bda32d56 2866 /* Set the "global" consumer URIs */
2f77fc4b 2867 for (i = 0; i < nb_uri; i++) {
e3876bf0
JR
2868 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2869 LTTNG_DOMAIN_NONE);
a74934ba 2870 if (ret != LTTNG_OK) {
2f77fc4b
DG
2871 goto error;
2872 }
2f77fc4b
DG
2873 }
2874
bda32d56
JG
2875 /* Set UST session URIs */
2876 if (session->ust_session) {
2877 for (i = 0; i < nb_uri; i++) {
b178f53e 2878 ret = add_uri_to_consumer(session,
bda32d56 2879 session->ust_session->consumer,
b178f53e 2880 &uris[i], LTTNG_DOMAIN_UST);
bda32d56
JG
2881 if (ret != LTTNG_OK) {
2882 goto error;
2883 }
2884 }
2885 }
2886
2887 /* Set kernel session URIs */
2888 if (session->kernel_session) {
2889 for (i = 0; i < nb_uri; i++) {
b178f53e 2890 ret = add_uri_to_consumer(session,
bda32d56 2891 session->kernel_session->consumer,
b178f53e 2892 &uris[i], LTTNG_DOMAIN_KERNEL);
bda32d56
JG
2893 if (ret != LTTNG_OK) {
2894 goto error;
2895 }
2896 }
2897 }
2898
7ab70fe0
DG
2899 /*
2900 * Make sure to set the session in output mode after we set URI since a
2901 * session can be created without URL (thus flagged in no output mode).
2902 */
2903 session->output_traces = 1;
2904 if (ksess) {
2905 ksess->output_traces = 1;
bda32d56
JG
2906 }
2907
2908 if (usess) {
7ab70fe0
DG
2909 usess->output_traces = 1;
2910 }
2911
2f77fc4b 2912 /* All good! */
f73fabfd 2913 ret = LTTNG_OK;
2f77fc4b
DG
2914
2915error:
2916 return ret;
2917}
2918
b178f53e
JG
2919static
2920enum lttng_error_code set_session_output_from_descriptor(
2921 struct ltt_session *session,
2922 const struct lttng_session_descriptor *descriptor)
2f77fc4b
DG
2923{
2924 int ret;
b178f53e
JG
2925 enum lttng_error_code ret_code = LTTNG_OK;
2926 enum lttng_session_descriptor_type session_type =
2927 lttng_session_descriptor_get_type(descriptor);
2928 enum lttng_session_descriptor_output_type output_type =
2929 lttng_session_descriptor_get_output_type(descriptor);
2930 struct lttng_uri uris[2] = {};
2931 size_t uri_count = 0;
2932
2933 switch (output_type) {
2934 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
2935 goto end;
2936 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
2937 lttng_session_descriptor_get_local_output_uri(descriptor,
2938 &uris[0]);
2939 uri_count = 1;
2940 break;
2941 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
2942 lttng_session_descriptor_get_network_output_uris(descriptor,
2943 &uris[0], &uris[1]);
2944 uri_count = 2;
2945 break;
2946 default:
2947 ret_code = LTTNG_ERR_INVALID;
e32d7f27 2948 goto end;
2f77fc4b
DG
2949 }
2950
b178f53e
JG
2951 switch (session_type) {
2952 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
2953 {
2954 struct snapshot_output *new_output = NULL;
2955
2956 new_output = snapshot_output_alloc();
2957 if (!new_output) {
2958 ret_code = LTTNG_ERR_NOMEM;
2959 goto end;
2960 }
2961
2962 ret = snapshot_output_init_with_uri(session,
2963 DEFAULT_SNAPSHOT_MAX_SIZE,
2964 NULL, uris, uri_count, session->consumer,
2965 new_output, &session->snapshot);
2966 if (ret < 0) {
2967 ret_code = (ret == -ENOMEM) ?
2968 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
2969 snapshot_output_destroy(new_output);
2970 goto end;
2971 }
2972 snapshot_add_output(&session->snapshot, new_output);
2973 break;
2974 }
2975 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
2976 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
2977 {
2978 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
2979 break;
2980 }
2981 default:
2982 ret_code = LTTNG_ERR_INVALID;
e32d7f27 2983 goto end;
2f77fc4b 2984 }
b178f53e
JG
2985end:
2986 return ret_code;
2987}
2988
2989static
2990enum lttng_error_code cmd_create_session_from_descriptor(
2991 struct lttng_session_descriptor *descriptor,
2992 const lttng_sock_cred *creds,
2993 const char *home_path)
2994{
2995 int ret;
2996 enum lttng_error_code ret_code;
2997 const char *session_name;
2998 struct ltt_session *new_session = NULL;
2999 enum lttng_session_descriptor_status descriptor_status;
2f77fc4b 3000
e32d7f27 3001 session_lock_list();
b178f53e
JG
3002 if (home_path) {
3003 if (*home_path != '/') {
3004 ERR("Home path provided by client is not absolute");
3005 ret_code = LTTNG_ERR_INVALID;
3006 goto end;
3007 }
3008 }
2f77fc4b 3009
b178f53e
JG
3010 descriptor_status = lttng_session_descriptor_get_session_name(
3011 descriptor, &session_name);
3012 switch (descriptor_status) {
3013 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3014 break;
3015 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3016 session_name = NULL;
3017 break;
3018 default:
3019 ret_code = LTTNG_ERR_INVALID;
3020 goto end;
3021 }
e3876bf0 3022
b178f53e 3023 ret_code = session_create(session_name, creds->uid, creds->gid,
e3876bf0 3024 &new_session);
b178f53e 3025 if (ret_code != LTTNG_OK) {
e32d7f27 3026 goto end;
2f77fc4b
DG
3027 }
3028
b178f53e
JG
3029 if (!session_name) {
3030 ret = lttng_session_descriptor_set_session_name(descriptor,
3031 new_session->name);
3032 if (ret) {
3033 ret_code = LTTNG_ERR_SESSION_FAIL;
3034 goto end;
3035 }
3036 }
3037
3038 if (!lttng_session_descriptor_is_output_destination_initialized(
3039 descriptor)) {
3040 /*
3041 * Only include the session's creation time in the output
3042 * destination if the name of the session itself was
3043 * not auto-generated.
3044 */
3045 ret_code = lttng_session_descriptor_set_default_output(
3046 descriptor,
3047 session_name ? &new_session->creation_time : NULL,
3048 home_path);
3049 if (ret_code != LTTNG_OK) {
e32d7f27 3050 goto end;
2bba9e53 3051 }
2bba9e53 3052 } else {
b178f53e
JG
3053 new_session->has_user_specified_directory =
3054 lttng_session_descriptor_has_output_directory(
3055 descriptor);
2f77fc4b
DG
3056 }
3057
b178f53e
JG
3058 switch (lttng_session_descriptor_get_type(descriptor)) {
3059 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3060 new_session->snapshot_mode = 1;
3061 break;
3062 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3063 new_session->live_timer =
3064 lttng_session_descriptor_live_get_timer_interval(
3065 descriptor);
3066 break;
3067 default:
3068 break;
3069 }
2f77fc4b 3070
b178f53e
JG
3071 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3072 if (ret_code != LTTNG_OK) {
3073 goto end;
3074 }
3075 new_session->consumer->enabled = 1;
3076 ret_code = LTTNG_OK;
e32d7f27 3077end:
b178f53e
JG
3078 /* Release reference provided by the session_create function. */
3079 session_put(new_session);
3080 if (ret_code != LTTNG_OK && new_session) {
3081 /* Release the global reference on error. */
3082 session_destroy(new_session);
e32d7f27 3083 }
b178f53e
JG
3084 session_unlock_list();
3085 return ret_code;
2f77fc4b
DG
3086}
3087
b178f53e
JG
3088enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3089 struct lttng_session_descriptor **return_descriptor)
27babd3a
DG
3090{
3091 int ret;
b178f53e
JG
3092 size_t payload_size;
3093 struct lttng_dynamic_buffer payload;
3094 struct lttng_buffer_view home_dir_view;
3095 struct lttng_buffer_view session_descriptor_view;
3096 struct lttng_session_descriptor *session_descriptor = NULL;
3097 enum lttng_error_code ret_code;
3098
3099 lttng_dynamic_buffer_init(&payload);
3a91de3a 3100 if (cmd_ctx->lsm.u.create_session.home_dir_size >=
b178f53e
JG
3101 LTTNG_PATH_MAX) {
3102 ret_code = LTTNG_ERR_INVALID;
3103 goto error;
27babd3a 3104 }
3a91de3a 3105 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
b178f53e
JG
3106 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3107 ret_code = LTTNG_ERR_INVALID;
3108 goto error;
27babd3a
DG
3109 }
3110
3a91de3a
JG
3111 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3112 cmd_ctx->lsm.u.create_session.session_descriptor_size;
b178f53e
JG
3113 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3114 if (ret) {
3115 ret_code = LTTNG_ERR_NOMEM;
3116 goto error;
27babd3a
DG
3117 }
3118
b178f53e
JG
3119 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3120 if (ret <= 0) {
3121 ERR("Reception of session descriptor failed, aborting.");
3122 ret_code = LTTNG_ERR_SESSION_FAIL;
3123 goto error;
27babd3a
DG
3124 }
3125
b178f53e
JG
3126 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3127 &payload,
3128 0,
3a91de3a 3129 cmd_ctx->lsm.u.create_session.home_dir_size);
3e6e0df2
JG
3130 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
3131 !lttng_buffer_view_is_valid(&home_dir_view)) {
3132 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3133 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3134 goto error;
3135 }
3136
b178f53e
JG
3137 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3138 &payload,
3a91de3a
JG
3139 cmd_ctx->lsm.u.create_session.home_dir_size,
3140 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3e6e0df2
JG
3141 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3142 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3143 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3144 goto error;
3145 }
27babd3a 3146
b178f53e
JG
3147 ret = lttng_session_descriptor_create_from_buffer(
3148 &session_descriptor_view, &session_descriptor);
3149 if (ret < 0) {
3150 ERR("Failed to create session descriptor from payload of \"create session\" command");
3151 ret_code = LTTNG_ERR_INVALID;
3152 goto error;
3153 }
27babd3a 3154
b178f53e
JG
3155 /*
3156 * Sets the descriptor's auto-generated properties (name, output) if
3157 * needed.
3158 */
3159 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3160 &cmd_ctx->creds,
3161 home_dir_view.size ? home_dir_view.data : NULL);
3162 if (ret_code != LTTNG_OK) {
3163 goto error;
e32d7f27 3164 }
b178f53e
JG
3165
3166 ret_code = LTTNG_OK;
3167 *return_descriptor = session_descriptor;
3168 session_descriptor = NULL;
3169error:
3170 lttng_dynamic_buffer_reset(&payload);
3171 lttng_session_descriptor_destroy(session_descriptor);
3172 return ret_code;
27babd3a
DG
3173}
3174
3e3665b8
JG
3175static
3176void cmd_destroy_session_reply(const struct ltt_session *session,
3177 void *_reply_context)
3178{
3179 int ret;
3180 ssize_t comm_ret;
3181 const struct cmd_destroy_session_reply_context *reply_context =
3182 _reply_context;
3183 struct lttng_dynamic_buffer payload;
3184 struct lttcomm_session_destroy_command_header cmd_header;
3185 struct lttng_trace_archive_location *location = NULL;
3186 struct lttcomm_lttng_msg llm = {
3187 .cmd_type = LTTNG_DESTROY_SESSION,
3285a971 3188 .ret_code = reply_context->destruction_status,
3e3665b8
JG
3189 .pid = UINT32_MAX,
3190 .cmd_header_size =
3191 sizeof(struct lttcomm_session_destroy_command_header),
3192 .data_size = 0,
3193 };
3194 size_t payload_size_before_location;
3195
3196 lttng_dynamic_buffer_init(&payload);
3197
3198 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
0e270a1e 3199 if (ret) {
3e3665b8
JG
3200 ERR("Failed to append session destruction message");
3201 goto error;
0e270a1e 3202 }
3e3665b8
JG
3203
3204 cmd_header.rotation_state =
3205 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3206 session->rotation_state :
3207 LTTNG_ROTATION_STATE_NO_ROTATION);
3208 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3209 sizeof(cmd_header));
3210 if (ret) {
3211 ERR("Failed to append session destruction command header");
3212 goto error;
3213 }
3214
3215 if (!reply_context->implicit_rotation_on_destroy) {
3216 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3217 session->name);
3218 goto send_reply;
3219 }
3220 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3221 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3222 session->name);
3223 goto send_reply;
3224 }
3225
3226 location = session_get_trace_archive_location(session);
3227 if (!location) {
3228 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3229 session->name);
3230 goto error;
3231 }
3232
3233 payload_size_before_location = payload.size;
3234 comm_ret = lttng_trace_archive_location_serialize(location,
3235 &payload);
3236 if (comm_ret < 0) {
3237 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3238 session->name);
3239 goto error;
3240 }
3241 /* Update the message to indicate the location's length. */
3242 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3243 payload.size - payload_size_before_location;
3244send_reply:
3245 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd,
3246 payload.data, payload.size);
3247 if (comm_ret != (ssize_t) payload.size) {
3248 ERR("Failed to send result of the destruction of session \"%s\" to client",
3249 session->name);
3250 }
3251error:
3252 ret = close(reply_context->reply_sock_fd);
3253 if (ret) {
3254 PERROR("Failed to close client socket in deferred session destroy reply");
3255 }
3256 lttng_dynamic_buffer_reset(&payload);
3257 free(_reply_context);
3258}
3259
2f77fc4b
DG
3260/*
3261 * Command LTTNG_DESTROY_SESSION processed by the client thread.
a9ad0c8f
MD
3262 *
3263 * Called with session lock held.
2f77fc4b 3264 */
e32d7f27 3265int cmd_destroy_session(struct ltt_session *session,
3e3665b8
JG
3266 struct notification_thread_handle *notification_thread_handle,
3267 int *sock_fd)
2f77fc4b
DG
3268{
3269 int ret;
3285a971 3270 enum lttng_error_code destruction_last_error = LTTNG_OK;
3e3665b8
JG
3271 struct cmd_destroy_session_reply_context *reply_context = NULL;
3272
3273 if (sock_fd) {
3274 reply_context = zmalloc(sizeof(*reply_context));
3275 if (!reply_context) {
3276 ret = LTTNG_ERR_NOMEM;
3277 goto end;
3278 }
3279 reply_context->reply_sock_fd = *sock_fd;
3280 }
2f77fc4b
DG
3281
3282 /* Safety net */
3283 assert(session);
3284
3e3665b8
JG
3285 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name,
3286 session->id);
3287 if (session->active) {
3288 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3289 session->name);
3290 ret = cmd_stop_trace(session);
3291 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3292 /* Carry on with the destruction of the session. */
3293 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3294 session->name, lttng_strerror(-ret));
3285a971 3295 destruction_last_error = ret;
3e3665b8
JG
3296 }
3297 }
5c408ad8 3298
92816cc3
JG
3299 if (session->rotation_schedule_timer_enabled) {
3300 if (timer_session_rotation_schedule_timer_stop(
3301 session)) {
3302 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3303 session->name);
3285a971 3304 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
92816cc3 3305 }
259c2674
JD
3306 }
3307
90936dcf
JD
3308 if (session->rotate_size) {
3309 unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
3310 session->rotate_size = 0;
3311 }
3312
a7ceb342 3313 if (session->rotated && session->current_trace_chunk && session->output_traces) {
b5893d8e
JG
3314 /*
3315 * Perform a last rotation on destruction if rotations have
3316 * occurred during the session's lifetime.
3317 */
343defc2
MD
3318 ret = cmd_rotate_session(session, NULL, false,
3319 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
d2956687
JG
3320 if (ret != LTTNG_OK) {
3321 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3322 session->name, lttng_strerror(-ret));
3285a971 3323 destruction_last_error = -ret;
124473a3 3324 }
0e270a1e 3325 if (reply_context) {
3e3665b8 3326 reply_context->implicit_rotation_on_destroy = true;
0e270a1e
JG
3327 }
3328 } else if (session->has_been_started && session->current_trace_chunk) {
7fdbed1c
JG
3329 /*
3330 * The user has not triggered a session rotation. However, to
3331 * ensure all data has been consumed, the session is rotated
3332 * to a 'null' trace chunk before it is destroyed.
3333 *
3334 * This is a "quiet" rotation meaning that no notification is
3335 * emitted and no renaming of the current trace chunk takes
3336 * place.
3337 */
343defc2
MD
3338 ret = cmd_rotate_session(session, NULL, true,
3339 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
53fb6336
JG
3340 /*
3341 * Rotation operations may not be supported by the kernel
3342 * tracer. Hence, do not consider this implicit rotation as
3343 * a session destruction error. The library has already stopped
3344 * the session and waited for pending data; there is nothing
3345 * left to do but complete the destruction of the session.
3346 */
3347 if (ret != LTTNG_OK &&
3348 ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
7fdbed1c 3349 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
53fb6336 3350 session->name, lttng_strerror(ret));
3285a971 3351 destruction_last_error = -ret;
7fdbed1c
JG
3352 }
3353 }
5c408ad8 3354
a503e1ef
JG
3355 if (session->shm_path[0]) {
3356 /*
3357 * When a session is created with an explicit shm_path,
3358 * the consumer daemon will create its shared memory files
3359 * at that location and will *not* unlink them. This is normal
3360 * as the intention of that feature is to make it possible
3361 * to retrieve the content of those files should a crash occur.
3362 *
3363 * To ensure the content of those files can be used, the
3364 * sessiond daemon will replicate the content of the metadata
3365 * cache in a metadata file.
3366 *
3367 * On clean-up, it is expected that the consumer daemon will
3368 * unlink the shared memory files and that the session daemon
3369 * will unlink the metadata file. Then, the session's directory
3370 * in the shm path can be removed.
3371 *
3372 * Unfortunately, a flaw in the design of the sessiond's and
3373 * consumerd's tear down of channels makes it impossible to
3374 * determine when the sessiond _and_ the consumerd have both
3375 * destroyed their representation of a channel. For one, the
3376 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3377 * callbacks in both daemons.
3378 *
3379 * However, it is also impossible for the sessiond to know when
3380 * the consumer daemon is done destroying its channel(s) since
3381 * it occurs as a reaction to the closing of the channel's file
3382 * descriptor. There is no resulting communication initiated
3383 * from the consumerd to the sessiond to confirm that the
3384 * operation is completed (and was successful).
3385 *
3386 * Until this is all fixed, the session daemon checks for the
3387 * removal of the session's shm path which makes it possible
3388 * to safely advertise a session as having been destroyed.
3389 *
3390 * Prior to this fix, it was not possible to reliably save
3391 * a session making use of the --shm-path option, destroy it,
3392 * and load it again. This is because the creation of the
3393 * session would fail upon seeing the session's shm path
3394 * already in existence.
3395 *
3396 * Note that none of the error paths in the check for the
3397 * directory's existence return an error. This is normal
3398 * as there isn't much that can be done. The session will
3399 * be destroyed properly, except that we can't offer the
3400 * guarantee that the same session can be re-created.
3401 */
3402 current_completion_handler = &destroy_completion_handler.handler;
3403 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3404 session->shm_path,
3405 sizeof(destroy_completion_handler.shm_path));
3406 assert(!ret);
3407 }
e32d7f27
JG
3408
3409 /*
3410 * The session is destroyed. However, note that the command context
3411 * still holds a reference to the session, thus delaying its destruction
3412 * _at least_ up to the point when that reference is released.
3413 */
3414 session_destroy(session);
3e3665b8 3415 if (reply_context) {
3285a971 3416 reply_context->destruction_status = destruction_last_error;
3e3665b8
JG
3417 ret = session_add_destroy_notifier(session,
3418 cmd_destroy_session_reply,
3419 (void *) reply_context);
3420 if (ret) {
3421 ret = LTTNG_ERR_FATAL;
3422 goto end;
3423 } else {
3424 *sock_fd = -1;
3425 }
0e270a1e
JG
3426 }
3427 ret = LTTNG_OK;
3e3665b8 3428end:
2f77fc4b
DG
3429 return ret;
3430}
3431
2f77fc4b
DG
3432/*
3433 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3434 */
56a37563
JG
3435int cmd_register_consumer(struct ltt_session *session,
3436 enum lttng_domain_type domain, const char *sock_path,
3437 struct consumer_data *cdata)
2f77fc4b
DG
3438{
3439 int ret, sock;
dd81b457 3440 struct consumer_socket *socket = NULL;
2f77fc4b
DG
3441
3442 assert(session);
3443 assert(cdata);
3444 assert(sock_path);
3445
3446 switch (domain) {
3447 case LTTNG_DOMAIN_KERNEL:
3448 {
3449 struct ltt_kernel_session *ksess = session->kernel_session;
3450
3451 assert(ksess);
3452
3453 /* Can't register a consumer if there is already one */
3454 if (ksess->consumer_fds_sent != 0) {
f73fabfd 3455 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
3456 goto error;
3457 }
3458
3459 sock = lttcomm_connect_unix_sock(sock_path);
3460 if (sock < 0) {
f73fabfd 3461 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
3462 goto error;
3463 }
4ce514c4 3464 cdata->cmd_sock = sock;
2f77fc4b 3465
4ce514c4 3466 socket = consumer_allocate_socket(&cdata->cmd_sock);
2f77fc4b 3467 if (socket == NULL) {
f66c074c
DG
3468 ret = close(sock);
3469 if (ret < 0) {
3470 PERROR("close register consumer");
3471 }
4ce514c4 3472 cdata->cmd_sock = -1;
f73fabfd 3473 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3474 goto error;
3475 }
3476
3477 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3478 if (socket->lock == NULL) {
3479 PERROR("zmalloc pthread mutex");
f73fabfd 3480 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3481 goto error;
3482 }
3483 pthread_mutex_init(socket->lock, NULL);
3484 socket->registered = 1;
3485
3486 rcu_read_lock();
3487 consumer_add_socket(socket, ksess->consumer);
3488 rcu_read_unlock();
3489
3490 pthread_mutex_lock(&cdata->pid_mutex);
3491 cdata->pid = -1;
3492 pthread_mutex_unlock(&cdata->pid_mutex);
3493
3494 break;
3495 }
3496 default:
3497 /* TODO: Userspace tracing */
f73fabfd 3498 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3499 goto error;
3500 }
3501
dd81b457 3502 return LTTNG_OK;
2f77fc4b
DG
3503
3504error:
dd81b457
DG
3505 if (socket) {
3506 consumer_destroy_socket(socket);
3507 }
2f77fc4b
DG
3508 return ret;
3509}
3510
3511/*
3512 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3513 */
3514ssize_t cmd_list_domains(struct ltt_session *session,
3515 struct lttng_domain **domains)
3516{
3517 int ret, index = 0;
3518 ssize_t nb_dom = 0;
fefd409b
DG
3519 struct agent *agt;
3520 struct lttng_ht_iter iter;
2f77fc4b
DG
3521
3522 if (session->kernel_session != NULL) {
3523 DBG3("Listing domains found kernel domain");
3524 nb_dom++;
3525 }
3526
3527 if (session->ust_session != NULL) {
3528 DBG3("Listing domains found UST global domain");
3529 nb_dom++;
3c6a091f 3530
e0a74f01 3531 rcu_read_lock();
fefd409b
DG
3532 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3533 agt, node.node) {
3534 if (agt->being_used) {
3535 nb_dom++;
3536 }
3c6a091f 3537 }
e0a74f01 3538 rcu_read_unlock();
2f77fc4b
DG
3539 }
3540
fa64dfb4
JG
3541 if (!nb_dom) {
3542 goto end;
3543 }
3544
2f77fc4b
DG
3545 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3546 if (*domains == NULL) {
f73fabfd 3547 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3548 goto error;
3549 }
3550
3551 if (session->kernel_session != NULL) {
3552 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
b5edb9e8
PP
3553
3554 /* Kernel session buffer type is always GLOBAL */
3555 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3556
2f77fc4b
DG
3557 index++;
3558 }
3559
3560 if (session->ust_session != NULL) {
3561 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 3562 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b 3563 index++;
3c6a091f 3564
e0a74f01 3565 rcu_read_lock();
fefd409b
DG
3566 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3567 agt, node.node) {
3568 if (agt->being_used) {
3569 (*domains)[index].type = agt->domain;
3570 (*domains)[index].buf_type = session->ust_session->buffer_type;
3571 index++;
3572 }
3c6a091f 3573 }
e0a74f01 3574 rcu_read_unlock();
2f77fc4b 3575 }
fa64dfb4 3576end:
2f77fc4b
DG
3577 return nb_dom;
3578
3579error:
f73fabfd
DG
3580 /* Return negative value to differentiate return code */
3581 return -ret;
2f77fc4b
DG
3582}
3583
3584
3585/*
3586 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3587 */
56a37563
JG
3588ssize_t cmd_list_channels(enum lttng_domain_type domain,
3589 struct ltt_session *session, struct lttng_channel **channels)
2f77fc4b 3590{
53e367f9 3591 ssize_t nb_chan = 0, payload_size = 0, ret;
2f77fc4b
DG
3592
3593 switch (domain) {
3594 case LTTNG_DOMAIN_KERNEL:
3595 if (session->kernel_session != NULL) {
3596 nb_chan = session->kernel_session->channel_count;
3597 }
3598 DBG3("Number of kernel channels %zd", nb_chan);
c7d620a2 3599 if (nb_chan <= 0) {
53e367f9
JG
3600 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
3601 goto end;
c7d620a2 3602 }
2f77fc4b
DG
3603 break;
3604 case LTTNG_DOMAIN_UST:
3605 if (session->ust_session != NULL) {
7ffc31a2 3606 rcu_read_lock();
2f77fc4b 3607 nb_chan = lttng_ht_get_count(
7ffc31a2
JG
3608 session->ust_session->domain_global.channels);
3609 rcu_read_unlock();
2f77fc4b
DG
3610 }
3611 DBG3("Number of UST global channels %zd", nb_chan);
ade7ce52 3612 if (nb_chan < 0) {
53e367f9
JG
3613 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
3614 goto end;
c7d620a2 3615 }
2f77fc4b
DG
3616 break;
3617 default:
53e367f9
JG
3618 ret = -LTTNG_ERR_UND;
3619 goto end;
2f77fc4b
DG
3620 }
3621
3622 if (nb_chan > 0) {
53e367f9 3623 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51
JG
3624 sizeof(struct lttng_channel_extended);
3625 struct lttng_channel_extended *channel_exts;
53e367f9
JG
3626
3627 payload_size = nb_chan * channel_size;
3628 *channels = zmalloc(payload_size);
2f77fc4b 3629 if (*channels == NULL) {
53e367f9
JG
3630 ret = -LTTNG_ERR_FATAL;
3631 goto end;
2f77fc4b
DG
3632 }
3633
53e367f9
JG
3634 channel_exts = ((void *) *channels) +
3635 (nb_chan * sizeof(struct lttng_channel));
d449df4a
MD
3636 ret = list_lttng_channels(domain, session, *channels, channel_exts);
3637 if (ret != LTTNG_OK) {
3638 free(*channels);
3639 *channels = NULL;
3640 goto end;
3641 }
53e367f9
JG
3642 } else {
3643 *channels = NULL;
2f77fc4b
DG
3644 }
3645
53e367f9
JG
3646 ret = payload_size;
3647end:
3648 return ret;
2f77fc4b
DG
3649}
3650
3651/*
3652 * Command LTTNG_LIST_EVENTS processed by the client thread.
3653 */
56a37563
JG
3654ssize_t cmd_list_events(enum lttng_domain_type domain,
3655 struct ltt_session *session, char *channel_name,
e368fb43 3656 struct lttng_payload *payload)
2f77fc4b
DG
3657{
3658 int ret = 0;
e368fb43
JG
3659 ssize_t nb_events = 0;
3660 struct lttcomm_event_command_header cmd_header = {};
3661 const size_t cmd_header_offset = payload->buffer.size;
3662
3663 ret = lttng_dynamic_buffer_append(
3664 &payload->buffer, &cmd_header, sizeof(cmd_header));
3665 if (ret) {
3666 ret = LTTNG_ERR_NOMEM;
3667 goto error;
3668 }
2f77fc4b
DG
3669
3670 switch (domain) {
3671 case LTTNG_DOMAIN_KERNEL:
3672 if (session->kernel_session != NULL) {
e368fb43
JG
3673 nb_events = list_lttng_kernel_events(channel_name,
3674 session->kernel_session, payload);
2f77fc4b
DG
3675 }
3676 break;
3677 case LTTNG_DOMAIN_UST:
3678 {
3679 if (session->ust_session != NULL) {
e368fb43
JG
3680 nb_events = list_lttng_ust_global_events(channel_name,
3681 &session->ust_session->domain_global,
3682 payload);
2f77fc4b
DG
3683 }
3684 break;
3685 }
5cdb6027 3686 case LTTNG_DOMAIN_LOG4J:
3c6a091f 3687 case LTTNG_DOMAIN_JUL:
0e115563 3688 case LTTNG_DOMAIN_PYTHON:
3c6a091f 3689 if (session->ust_session) {
fefd409b
DG
3690 struct lttng_ht_iter iter;
3691 struct agent *agt;
3692
b11feea5 3693 rcu_read_lock();
fefd409b
DG
3694 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3695 &iter.iter, agt, node.node) {
1dfd9906 3696 if (agt->domain == domain) {
e368fb43
JG
3697 nb_events = list_lttng_agent_events(
3698 agt, payload);
1dfd9906
JG
3699 break;
3700 }
fefd409b 3701 }
b11feea5 3702 rcu_read_unlock();
3c6a091f
DG
3703 }
3704 break;
2f77fc4b 3705 default:
f73fabfd 3706 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3707 goto error;
3708 }
3709
e368fb43
JG
3710 ((struct lttcomm_event_command_header *) (payload->buffer.data +
3711 cmd_header_offset))->nb_events = (uint32_t) nb_events;
3712
3713 return nb_events;
2f77fc4b
DG
3714
3715error:
f73fabfd
DG
3716 /* Return negative value to differentiate return code */
3717 return -ret;
2f77fc4b
DG
3718}
3719
3720/*
3721 * Using the session list, filled a lttng_session array to send back to the
3722 * client for session listing.
3723 *
3724 * The session list lock MUST be acquired before calling this function. Use
3725 * session_lock_list() and session_unlock_list().
3726 */
b178f53e
JG
3727void cmd_list_lttng_sessions(struct lttng_session *sessions,
3728 size_t session_count, uid_t uid, gid_t gid)
2f77fc4b
DG
3729{
3730 int ret;
3731 unsigned int i = 0;
3732 struct ltt_session *session;
3733 struct ltt_session_list *list = session_get_list();
0e270a1e 3734 struct lttng_session_extended *extended =
b178f53e 3735 (typeof(extended)) (&sessions[session_count]);
2f77fc4b
DG
3736
3737 DBG("Getting all available session for UID %d GID %d",
3738 uid, gid);
3739 /*
3740 * Iterate over session list and append data after the control struct in
3741 * the buffer.
3742 */
3743 cds_list_for_each_entry(session, &list->head, list) {
e32d7f27
JG
3744 if (!session_get(session)) {
3745 continue;
3746 }
2f77fc4b
DG
3747 /*
3748 * Only list the sessions the user can control.
3749 */
d7b377ed 3750 if (!session_access_ok(session, uid) ||
e32d7f27
JG
3751 session->destroyed) {
3752 session_put(session);
2f77fc4b
DG
3753 continue;
3754 }
3755
3756 struct ltt_kernel_session *ksess = session->kernel_session;
3757 struct ltt_ust_session *usess = session->ust_session;
3758
3759 if (session->consumer->type == CONSUMER_DST_NET ||
3760 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3761 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3762 ret = build_network_session_path(sessions[i].path,
dec56f6c 3763 sizeof(sessions[i].path), session);
2f77fc4b 3764 } else {
dec56f6c 3765 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
366a9222 3766 session->consumer->dst.session_root_path);
2f77fc4b
DG
3767 }
3768 if (ret < 0) {
3769 PERROR("snprintf session path");
e32d7f27 3770 session_put(session);
2f77fc4b
DG
3771 continue;
3772 }
3773
3774 strncpy(sessions[i].name, session->name, NAME_MAX);
3775 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 3776 sessions[i].enabled = session->active;
2cbf8fed 3777 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 3778 sessions[i].live_timer_interval = session->live_timer;
b178f53e
JG
3779 extended[i].creation_time.value = (uint64_t) session->creation_time;
3780 extended[i].creation_time.is_set = 1;
2f77fc4b 3781 i++;
e32d7f27 3782 session_put(session);
2f77fc4b
DG
3783 }
3784}
3785
806e2684 3786/*
6d805429 3787 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 3788 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 3789 */
6d805429 3790int cmd_data_pending(struct ltt_session *session)
806e2684
DG
3791{
3792 int ret;
3793 struct ltt_kernel_session *ksess = session->kernel_session;
3794 struct ltt_ust_session *usess = session->ust_session;
3795
3796 assert(session);
3797
5c408ad8
JD
3798 DBG("Data pending for session %s", session->name);
3799
806e2684 3800 /* Session MUST be stopped to ask for data availability. */
8382cf6f 3801 if (session->active) {
806e2684
DG
3802 ret = LTTNG_ERR_SESSION_STARTED;
3803 goto error;
3a89d11a
DG
3804 } else {
3805 /*
3806 * If stopped, just make sure we've started before else the above call
3807 * will always send that there is data pending.
3808 *
3809 * The consumer assumes that when the data pending command is received,
3810 * the trace has been started before or else no output data is written
3811 * by the streams which is a condition for data pending. So, this is
3812 * *VERY* important that we don't ask the consumer before a start
3813 * trace.
3814 */
8382cf6f 3815 if (!session->has_been_started) {
3a89d11a
DG
3816 ret = 0;
3817 goto error;
3818 }
806e2684
DG
3819 }
3820
92816cc3
JG
3821 /* A rotation is still pending, we have to wait. */
3822 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5c408ad8
JD
3823 DBG("Rotate still pending for session %s", session->name);
3824 ret = 1;
3825 goto error;
3826 }
3827
806e2684 3828 if (ksess && ksess->consumer) {
6d805429
DG
3829 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3830 if (ret == 1) {
806e2684
DG
3831 /* Data is still being extracted for the kernel. */
3832 goto error;
3833 }
3834 }
3835
3836 if (usess && usess->consumer) {
6d805429
DG
3837 ret = consumer_is_data_pending(usess->id, usess->consumer);
3838 if (ret == 1) {
806e2684
DG
3839 /* Data is still being extracted for the kernel. */
3840 goto error;
3841 }
3842 }
3843
3844 /* Data is ready to be read by a viewer */
6d805429 3845 ret = 0;
806e2684
DG
3846
3847error:
3848 return ret;
3849}
3850
6dc3064a
DG
3851/*
3852 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3853 *
3854 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3855 */
3856int cmd_snapshot_add_output(struct ltt_session *session,
df4f5a87 3857 const struct lttng_snapshot_output *output, uint32_t *id)
6dc3064a
DG
3858{
3859 int ret;
3860 struct snapshot_output *new_output;
3861
3862 assert(session);
3863 assert(output);
3864
3865 DBG("Cmd snapshot add output for session %s", session->name);
3866
3867 /*
903ef685 3868 * Can't create an output if the session is not set in no-output mode.
6dc3064a
DG
3869 */
3870 if (session->output_traces) {
903ef685 3871 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3872 goto error;
3873 }
3874
54213acc
JG
3875 if (session->has_non_mmap_channel) {
3876 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
3877 goto error;
3878 }
3879
6dc3064a
DG
3880 /* Only one output is allowed until we have the "tee" feature. */
3881 if (session->snapshot.nb_output == 1) {
3882 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3883 goto error;
3884 }
3885
3886 new_output = snapshot_output_alloc();
3887 if (!new_output) {
3888 ret = LTTNG_ERR_NOMEM;
3889 goto error;
3890 }
3891
b178f53e 3892 ret = snapshot_output_init(session, output->max_size, output->name,
6dc3064a
DG
3893 output->ctrl_url, output->data_url, session->consumer, new_output,
3894 &session->snapshot);
3895 if (ret < 0) {
3896 if (ret == -ENOMEM) {
3897 ret = LTTNG_ERR_NOMEM;
3898 } else {
3899 ret = LTTNG_ERR_INVALID;
3900 }
3901 goto free_error;
3902 }
3903
6dc3064a
DG
3904 rcu_read_lock();
3905 snapshot_add_output(&session->snapshot, new_output);
3906 if (id) {
3907 *id = new_output->id;
3908 }
3909 rcu_read_unlock();
3910
3911 return LTTNG_OK;
3912
3913free_error:
3914 snapshot_output_destroy(new_output);
3915error:
3916 return ret;
3917}
3918
3919/*
3920 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3921 *
3922 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3923 */
3924int cmd_snapshot_del_output(struct ltt_session *session,
df4f5a87 3925 const struct lttng_snapshot_output *output)
6dc3064a
DG
3926{
3927 int ret;
eb240553 3928 struct snapshot_output *sout = NULL;
6dc3064a
DG
3929
3930 assert(session);
3931 assert(output);
3932
6dc3064a
DG
3933 rcu_read_lock();
3934
3935 /*
d3f14b8a
MD
3936 * Permission denied to create an output if the session is not
3937 * set in no output mode.
6dc3064a
DG
3938 */
3939 if (session->output_traces) {
903ef685 3940 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3941 goto error;
3942 }
3943
eb240553
DG
3944 if (output->id) {
3945 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3946 session->name);
3947 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3948 } else if (*output->name != '\0') {
3949 DBG("Cmd snapshot del output name %s for session %s", output->name,
3950 session->name);
3951 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3952 }
6dc3064a
DG
3953 if (!sout) {
3954 ret = LTTNG_ERR_INVALID;
3955 goto error;
3956 }
3957
3958 snapshot_delete_output(&session->snapshot, sout);
3959 snapshot_output_destroy(sout);
3960 ret = LTTNG_OK;
3961
3962error:
3963 rcu_read_unlock();
3964 return ret;
3965}
3966
3967/*
3968 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3969 *
3970 * If no output is available, outputs is untouched and 0 is returned.
3971 *
3972 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3973 */
3974ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3975 struct lttng_snapshot_output **outputs)
3976{
3977 int ret, idx = 0;
b223ca94 3978 struct lttng_snapshot_output *list = NULL;
6dc3064a
DG
3979 struct lttng_ht_iter iter;
3980 struct snapshot_output *output;
3981
3982 assert(session);
3983 assert(outputs);
3984
3985 DBG("Cmd snapshot list outputs for session %s", session->name);
3986
3987 /*
d3f14b8a
MD
3988 * Permission denied to create an output if the session is not
3989 * set in no output mode.
6dc3064a
DG
3990 */
3991 if (session->output_traces) {
903ef685
JG
3992 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3993 goto end;
6dc3064a
DG
3994 }
3995
3996 if (session->snapshot.nb_output == 0) {
3997 ret = 0;
903ef685 3998 goto end;
6dc3064a
DG
3999 }
4000
4001 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
4002 if (!list) {
b223ca94 4003 ret = -LTTNG_ERR_NOMEM;
903ef685 4004 goto end;
6dc3064a
DG
4005 }
4006
4007 /* Copy list from session to the new list object. */
b223ca94 4008 rcu_read_lock();
6dc3064a
DG
4009 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
4010 output, node.node) {
4011 assert(output->consumer);
4012 list[idx].id = output->id;
4013 list[idx].max_size = output->max_size;
6ce22875
MD
4014 if (lttng_strncpy(list[idx].name, output->name,
4015 sizeof(list[idx].name))) {
4016 ret = -LTTNG_ERR_INVALID;
903ef685 4017 goto error;
6ce22875 4018 }
6dc3064a 4019 if (output->consumer->type == CONSUMER_DST_LOCAL) {
6ce22875 4020 if (lttng_strncpy(list[idx].ctrl_url,
366a9222 4021 output->consumer->dst.session_root_path,
6ce22875
MD
4022 sizeof(list[idx].ctrl_url))) {
4023 ret = -LTTNG_ERR_INVALID;
903ef685 4024 goto error;
6ce22875 4025 }
6dc3064a
DG
4026 } else {
4027 /* Control URI. */
4028 ret = uri_to_str_url(&output->consumer->dst.net.control,
4029 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
4030 if (ret < 0) {
b223ca94 4031 ret = -LTTNG_ERR_NOMEM;
903ef685 4032 goto error;
6dc3064a
DG
4033 }
4034
4035 /* Data URI. */
4036 ret = uri_to_str_url(&output->consumer->dst.net.data,
4037 list[idx].data_url, sizeof(list[idx].data_url));
4038 if (ret < 0) {
b223ca94 4039 ret = -LTTNG_ERR_NOMEM;
903ef685 4040 goto error;
6dc3064a
DG
4041 }
4042 }
4043 idx++;
4044 }
4045
4046 *outputs = list;
b223ca94
JG
4047 list = NULL;
4048 ret = session->snapshot.nb_output;
6dc3064a 4049error:
903ef685 4050 rcu_read_unlock();
b223ca94 4051 free(list);
903ef685 4052end:
b223ca94 4053 return ret;
6dc3064a
DG
4054}
4055
93ec662e
JD
4056/*
4057 * Check if we can regenerate the metadata for this session.
4058 * Only kernel, UST per-uid and non-live sessions are supported.
4059 *
4060 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4061 */
4062static
eded6438 4063int check_regenerate_metadata_support(struct ltt_session *session)
93ec662e
JD
4064{
4065 int ret;
4066
4067 assert(session);
4068
4069 if (session->live_timer != 0) {
4070 ret = LTTNG_ERR_LIVE_SESSION;
4071 goto end;
4072 }
4073 if (!session->active) {
4074 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4075 goto end;
4076 }
4077 if (session->ust_session) {
4078 switch (session->ust_session->buffer_type) {
4079 case LTTNG_BUFFER_PER_UID:
4080 break;
4081 case LTTNG_BUFFER_PER_PID:
4082 ret = LTTNG_ERR_PER_PID_SESSION;
4083 goto end;
4084 default:
4085 assert(0);
4086 ret = LTTNG_ERR_UNK;
4087 goto end;
4088 }
4089 }
4090 if (session->consumer->type == CONSUMER_DST_NET &&
4091 session->consumer->relay_minor_version < 8) {
4092 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4093 goto end;
4094 }
4095 ret = 0;
4096
4097end:
4098 return ret;
4099}
4100
7802bae3
LL
4101static
4102int clear_metadata_file(int fd)
4103{
4104 int ret;
a5df8828 4105 off_t lseek_ret;
7802bae3 4106
a5df8828
GL
4107 lseek_ret = lseek(fd, 0, SEEK_SET);
4108 if (lseek_ret < 0) {
7802bae3 4109 PERROR("lseek");
a5df8828 4110 ret = -1;
7802bae3
LL
4111 goto end;
4112 }
4113
4114 ret = ftruncate(fd, 0);
4115 if (ret < 0) {
4116 PERROR("ftruncate");
4117 goto end;
4118 }
4119
4120end:
4121 return ret;
4122}
4123
93ec662e 4124static
eded6438 4125int ust_regenerate_metadata(struct ltt_ust_session *usess)
93ec662e
JD
4126{
4127 int ret = 0;
4128 struct buffer_reg_uid *uid_reg = NULL;
4129 struct buffer_reg_session *session_reg = NULL;
4130
4131 rcu_read_lock();
4132 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
4133 struct ust_registry_session *registry;
4134 struct ust_registry_channel *chan;
4135 struct lttng_ht_iter iter_chan;
4136
4137 session_reg = uid_reg->registry;
4138 registry = session_reg->reg.ust;
4139
4140 pthread_mutex_lock(&registry->lock);
4141 registry->metadata_len_sent = 0;
4142 memset(registry->metadata, 0, registry->metadata_alloc_len);
4143 registry->metadata_len = 0;
4144 registry->metadata_version++;
7802bae3
LL
4145 if (registry->metadata_fd > 0) {
4146 /* Clear the metadata file's content. */
4147 ret = clear_metadata_file(registry->metadata_fd);
4148 if (ret) {
4149 pthread_mutex_unlock(&registry->lock);
4150 goto end;
4151 }
4152 }
4153
93ec662e
JD
4154 ret = ust_metadata_session_statedump(registry, NULL,
4155 registry->major, registry->minor);
4156 if (ret) {
4157 pthread_mutex_unlock(&registry->lock);
4158 ERR("Failed to generate session metadata (err = %d)",
4159 ret);
4160 goto end;
4161 }
4162 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
4163 chan, node.node) {
4164 struct ust_registry_event *event;
4165 struct lttng_ht_iter iter_event;
4166
4167 ret = ust_metadata_channel_statedump(registry, chan);
4168 if (ret) {
4169 pthread_mutex_unlock(&registry->lock);
4170 ERR("Failed to generate channel metadata "
4171 "(err = %d)", ret);
4172 goto end;
4173 }
4174 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
4175 event, node.node) {
4176 ret = ust_metadata_event_statedump(registry,
4177 chan, event);
4178 if (ret) {
4179 pthread_mutex_unlock(&registry->lock);
4180 ERR("Failed to generate event metadata "
4181 "(err = %d)", ret);
4182 goto end;
4183 }
4184 }
4185 }
4186 pthread_mutex_unlock(&registry->lock);
4187 }
4188
4189end:
4190 rcu_read_unlock();
4191 return ret;
4192}
4193
4194/*
eded6438 4195 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
93ec662e
JD
4196 *
4197 * Ask the consumer to truncate the existing metadata file(s) and
4198 * then regenerate the metadata. Live and per-pid sessions are not
4199 * supported and return an error.
4200 *
4201 * Return 0 on success or else a LTTNG_ERR code.
4202 */
eded6438 4203int cmd_regenerate_metadata(struct ltt_session *session)
93ec662e
JD
4204{
4205 int ret;
4206
4207 assert(session);
4208
eded6438 4209 ret = check_regenerate_metadata_support(session);
93ec662e
JD
4210 if (ret) {
4211 goto end;
4212 }
4213
4214 if (session->kernel_session) {
eded6438 4215 ret = kernctl_session_regenerate_metadata(
93ec662e
JD
4216 session->kernel_session->fd);
4217 if (ret < 0) {
4218 ERR("Failed to regenerate the kernel metadata");
4219 goto end;
4220 }
4221 }
4222
4223 if (session->ust_session) {
eded6438 4224 ret = ust_regenerate_metadata(session->ust_session);
93ec662e
JD
4225 if (ret < 0) {
4226 ERR("Failed to regenerate the UST metadata");
4227 goto end;
4228 }
4229 }
4230 DBG("Cmd metadata regenerate for session %s", session->name);
4231 ret = LTTNG_OK;
4232
4233end:
4234 return ret;
4235}
4236
c2561365
JD
4237/*
4238 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4239 *
4240 * Ask the tracer to regenerate a new statedump.
4241 *
4242 * Return 0 on success or else a LTTNG_ERR code.
4243 */
4244int cmd_regenerate_statedump(struct ltt_session *session)
4245{
4246 int ret;
4247
4248 assert(session);
4249
4250 if (!session->active) {
4251 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4252 goto end;
4253 }
c2561365
JD
4254
4255 if (session->kernel_session) {
4256 ret = kernctl_session_regenerate_statedump(
4257 session->kernel_session->fd);
4258 /*
4259 * Currently, the statedump in kernel can only fail if out
4260 * of memory.
4261 */
4262 if (ret < 0) {
4263 if (ret == -ENOMEM) {
4264 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4265 } else {
4266 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4267 }
4268 ERR("Failed to regenerate the kernel statedump");
4269 goto end;
4270 }
4271 }
4272
4273 if (session->ust_session) {
4274 ret = ust_app_regenerate_statedump_all(session->ust_session);
4275 /*
4276 * Currently, the statedump in UST always returns 0.
4277 */
4278 if (ret < 0) {
4279 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4280 ERR("Failed to regenerate the UST statedump");
4281 goto end;
4282 }
4283 }
4284 DBG("Cmd regenerate statedump for session %s", session->name);
4285 ret = LTTNG_OK;
4286
4287end:
4288 return ret;
4289}
4290
b0880ae5 4291int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock,
242388e4
JR
4292 struct notification_thread_handle *notification_thread,
4293 struct lttng_trigger **return_trigger)
b0880ae5
JG
4294{
4295 int ret;
4296 size_t trigger_len;
4297 ssize_t sock_recv_len;
4298 struct lttng_trigger *trigger = NULL;
c0a66c84 4299 struct lttng_payload trigger_payload;
3da864a9 4300 struct lttng_credentials cmd_creds = {
ff588497
JR
4301 .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
4302 .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
3da864a9 4303 };
b0880ae5 4304
c0a66c84 4305 lttng_payload_init(&trigger_payload);
3a91de3a 4306 trigger_len = (size_t) cmd_ctx->lsm.u.trigger.length;
c0a66c84
JG
4307 ret = lttng_dynamic_buffer_set_size(
4308 &trigger_payload.buffer, trigger_len);
b0880ae5
JG
4309 if (ret) {
4310 ret = LTTNG_ERR_NOMEM;
4311 goto end;
4312 }
4313
c0a66c84
JG
4314 sock_recv_len = lttcomm_recv_unix_sock(
4315 sock, trigger_payload.buffer.data, trigger_len);
b0880ae5
JG
4316 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
4317 ERR("Failed to receive \"register trigger\" command payload");
76992d4d 4318 ret = LTTNG_ERR_INVALID_PROTOCOL;
b0880ae5
JG
4319 goto end;
4320 }
4321
99608320
JR
4322 /* Receive fds, if any. */
4323 if (cmd_ctx->lsm.fd_count > 0) {
4324 ret = lttcomm_recv_payload_fds_unix_sock(
4325 sock, cmd_ctx->lsm.fd_count, &trigger_payload);
4326 if (ret > 0 && ret != cmd_ctx->lsm.fd_count * sizeof(int)) {
4327 ret = LTTNG_ERR_INVALID_PROTOCOL;
4328 goto end;
4329 } else if (ret <= 0) {
4330 ret = LTTNG_ERR_FATAL;
4331 goto end;
4332 }
4333 }
4334
4335 /* Deserialize trigger. */
c0a66c84
JG
4336 {
4337 struct lttng_payload_view view =
4338 lttng_payload_view_from_payload(
4339 &trigger_payload, 0, -1);
4340
4341 if (lttng_trigger_create_from_payload(&view, &trigger) !=
4342 trigger_len) {
4343 ERR("Invalid trigger payload received in \"register trigger\" command");
4344 ret = LTTNG_ERR_INVALID_TRIGGER;
4345 goto end;
4346 }
b0880ae5
JG
4347 }
4348
64eafdf6
JR
4349 /*
4350 * Validate the trigger credentials against the command credentials.
4351 * Only the root user can register a trigger with non-matching
4352 * credentials.
4353 */
4354 if (!lttng_credentials_is_equal_uid(
4355 lttng_trigger_get_credentials(trigger),
4356 &cmd_creds)) {
4357 if (lttng_credentials_get_uid(&cmd_creds) != 0) {
4358 ERR("Trigger credentials do not match the command credentials");
4359 ret = LTTNG_ERR_INVALID_TRIGGER;
4360 goto end;
4361 }
4362 }
3da864a9 4363
58daac01
JR
4364 /*
4365 * The bytecode generation also serves as a validation step for the
4366 * bytecode expressions.
4367 */
4368 ret = lttng_trigger_generate_bytecode(trigger, &cmd_creds);
4369 if (ret != LTTNG_OK) {
4370 goto end;
4371 }
4372
242388e4
JR
4373 /*
4374 * A reference to the trigger is acquired by the notification thread.
4375 * It is safe to return the same trigger to the caller since it the
4376 * other user holds a reference.
4377 *
4378 * The trigger is modified during the execution of the
4379 * "register trigger" command. However, by the time the command returns,
4380 * it is safe to use without any locking as its properties are
4381 * immutable.
4382 */
b0880ae5
JG
4383 ret = notification_thread_command_register_trigger(notification_thread,
4384 trigger);
242388e4
JR
4385 if (ret != LTTNG_OK) {
4386 goto end_notification_thread;
4387 }
4388
4389 /* Return an updated trigger to the client. */
4390 *return_trigger = trigger;
4391
4392end_notification_thread:
587f9fd0
JG
4393 /* Ownership of trigger was transferred. */
4394 trigger = NULL;
b0880ae5 4395end:
587f9fd0 4396 lttng_trigger_destroy(trigger);
c0a66c84 4397 lttng_payload_reset(&trigger_payload);
b0880ae5
JG
4398 return ret;
4399}
4400
4401int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock,
4402 struct notification_thread_handle *notification_thread)
4403{
4404 int ret;
4405 size_t trigger_len;
4406 ssize_t sock_recv_len;
4407 struct lttng_trigger *trigger = NULL;
c0a66c84 4408 struct lttng_payload trigger_payload;
3da864a9 4409 struct lttng_credentials cmd_creds = {
ff588497
JR
4410 .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
4411 .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
3da864a9 4412 };
b0880ae5 4413
c0a66c84 4414 lttng_payload_init(&trigger_payload);
3a91de3a 4415 trigger_len = (size_t) cmd_ctx->lsm.u.trigger.length;
c0a66c84
JG
4416 ret = lttng_dynamic_buffer_set_size(
4417 &trigger_payload.buffer, trigger_len);
b0880ae5
JG
4418 if (ret) {
4419 ret = LTTNG_ERR_NOMEM;
4420 goto end;
4421 }
4422
c0a66c84
JG
4423 sock_recv_len = lttcomm_recv_unix_sock(
4424 sock, trigger_payload.buffer.data, trigger_len);
b0880ae5
JG
4425 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
4426 ERR("Failed to receive \"unregister trigger\" command payload");
4427 /* TODO: should this be a new error enum ? */
4428 ret = LTTNG_ERR_INVALID_TRIGGER;
4429 goto end;
4430 }
4431
99608320
JR
4432 /* Receive fds, if any. */
4433 if (cmd_ctx->lsm.fd_count > 0) {
4434 ret = lttcomm_recv_payload_fds_unix_sock(
4435 sock, cmd_ctx->lsm.fd_count, &trigger_payload);
4436 if (ret > 0 && ret != cmd_ctx->lsm.fd_count * sizeof(int)) {
4437 ret = LTTNG_ERR_INVALID_PROTOCOL;
4438 goto end;
4439 } else if (ret <= 0) {
4440 ret = LTTNG_ERR_FATAL;
4441 goto end;
4442 }
4443 }
4444
c0a66c84
JG
4445 {
4446 struct lttng_payload_view view =
4447 lttng_payload_view_from_payload(
4448 &trigger_payload, 0, -1);
4449
4450 if (lttng_trigger_create_from_payload(&view, &trigger) !=
4451 trigger_len) {
4452 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4453 ret = LTTNG_ERR_INVALID_TRIGGER;
4454 goto end;
4455 }
b0880ae5
JG
4456 }
4457
64eafdf6
JR
4458 /*
4459 * Validate the trigger credentials against the command credentials.
4460 * Only the root user can unregister a trigger with non-matching
4461 * credentials.
4462 */
4463 if (!lttng_credentials_is_equal_uid(
4464 lttng_trigger_get_credentials(trigger),
4465 &cmd_creds)) {
4466 if (lttng_credentials_get_uid(&cmd_creds) != 0) {
4467 ERR("Trigger credentials do not match the command credentials");
4468 ret = LTTNG_ERR_INVALID_TRIGGER;
4469 goto end;
4470 }
4471 }
3da864a9 4472
b0880ae5
JG
4473 ret = notification_thread_command_unregister_trigger(notification_thread,
4474 trigger);
4475end:
587f9fd0 4476 lttng_trigger_destroy(trigger);
c0a66c84 4477 lttng_payload_reset(&trigger_payload);
b0880ae5
JG
4478 return ret;
4479}
4480
fbc9f37d
JR
4481int cmd_list_triggers(struct command_ctx *cmd_ctx,
4482 struct notification_thread_handle *notification_thread,
4483 struct lttng_triggers **return_triggers)
4484{
4485 int ret = 0;
4486 enum lttng_error_code ret_code;
4487 struct lttng_triggers *triggers = NULL;
4488
4489 /* Get the set of triggers from the notification thread. */
4490 ret_code = notification_thread_command_list_triggers(
4491 notification_thread, cmd_ctx->creds.uid, &triggers);
4492 if (ret_code != LTTNG_OK) {
4493 ret = ret_code;
4494 goto end;
4495 }
4496
4497 *return_triggers = triggers;
4498 triggers = NULL;
4499 ret = LTTNG_OK;
4500end:
4501 lttng_triggers_destroy(triggers);
4502 return ret;
4503}
6dc3064a
DG
4504/*
4505 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4506 * snapshot output is *not* set with a remote destination.
4507 *
9a654598 4508 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 4509 */
9a654598 4510static enum lttng_error_code set_relayd_for_snapshot(
348a81dc 4511 struct consumer_output *output,
fb9a95c4 4512 const struct ltt_session *session)
6dc3064a 4513{
9a654598 4514 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
4515 struct lttng_ht_iter iter;
4516 struct consumer_socket *socket;
1e791a74 4517 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
6fa5fe7c 4518 const char *base_path;
6dc3064a 4519
348a81dc 4520 assert(output);
6dc3064a
DG
4521 assert(session);
4522
4523 DBG2("Set relayd object from snapshot output");
4524
1e791a74 4525 if (session->current_trace_chunk) {
348a81dc
JG
4526 enum lttng_trace_chunk_status chunk_status =
4527 lttng_trace_chunk_get_id(
4528 session->current_trace_chunk,
4529 &current_chunk_id.value);
1e791a74 4530
348a81dc 4531 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1e791a74
JG
4532 current_chunk_id.is_set = true;
4533 } else {
4534 ERR("Failed to get current trace chunk id");
4535 status = LTTNG_ERR_UNK;
4536 goto error;
4537 }
4538 }
4539
6dc3064a 4540 /* Ignore if snapshot consumer output is not network. */
348a81dc 4541 if (output->type != CONSUMER_DST_NET) {
6dc3064a
DG
4542 goto error;
4543 }
4544
6fa5fe7c
MD
4545 /*
4546 * The snapshot record URI base path overrides the session
4547 * base path.
4548 */
4549 if (output->dst.net.control.subdir[0] != '\0') {
4550 base_path = output->dst.net.control.subdir;
4551 } else {
4552 base_path = session->base_path;
4553 }
4554
6dc3064a
DG
4555 /*
4556 * For each consumer socket, create and send the relayd object of the
4557 * snapshot output.
4558 */
4559 rcu_read_lock();
348a81dc 4560 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
5eecee74 4561 socket, node.node) {
ecd0f96d 4562 pthread_mutex_lock(socket->lock);
9a654598 4563 status = send_consumer_relayd_sockets(0, session->id,
348a81dc 4564 output, socket,
d3e2ba59 4565 session->name, session->hostname,
6fa5fe7c 4566 base_path,
1e791a74 4567 session->live_timer,
db1da059 4568 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
46ef2188
MD
4569 session->creation_time,
4570 session->name_contains_creation_time);
ecd0f96d 4571 pthread_mutex_unlock(socket->lock);
9a654598 4572 if (status != LTTNG_OK) {
6dc3064a
DG
4573 rcu_read_unlock();
4574 goto error;
4575 }
4576 }
4577 rcu_read_unlock();
4578
4579error:
9a654598 4580 return status;
6dc3064a
DG
4581}
4582
4583/*
4584 * Record a kernel snapshot.
4585 *
fac41e72 4586 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 4587 */
fb9a95c4
JG
4588static enum lttng_error_code record_kernel_snapshot(
4589 struct ltt_kernel_session *ksess,
348a81dc 4590 const struct consumer_output *output,
fb9a95c4 4591 const struct ltt_session *session,
d07ceecd 4592 int wait, uint64_t nb_packets_per_stream)
6dc3064a 4593{
9a654598 4594 enum lttng_error_code status;
6dc3064a
DG
4595
4596 assert(ksess);
4597 assert(output);
4598 assert(session);
4599
348a81dc
JG
4600 status = kernel_snapshot_record(
4601 ksess, output, wait, nb_packets_per_stream);
9a654598 4602 return status;
6dc3064a
DG
4603}
4604
4605/*
4606 * Record a UST snapshot.
4607 *
9a654598 4608 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 4609 */
9a654598 4610static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
348a81dc
JG
4611 const struct consumer_output *output,
4612 const struct ltt_session *session,
4613 int wait, uint64_t nb_packets_per_stream)
6dc3064a 4614{
9a654598 4615 enum lttng_error_code status;
6dc3064a
DG
4616
4617 assert(usess);
4618 assert(output);
4619 assert(session);
4620
348a81dc
JG
4621 status = ust_app_snapshot_record(
4622 usess, output, wait, nb_packets_per_stream);
9a654598 4623 return status;
6dc3064a
DG
4624}
4625
d07ceecd 4626static
fb9a95c4
JG
4627uint64_t get_session_size_one_more_packet_per_stream(
4628 const struct ltt_session *session, uint64_t cur_nr_packets)
68808f4e 4629{
d07ceecd 4630 uint64_t tot_size = 0;
68808f4e
DG
4631
4632 if (session->kernel_session) {
4633 struct ltt_kernel_channel *chan;
fb9a95c4
JG
4634 const struct ltt_kernel_session *ksess =
4635 session->kernel_session;
68808f4e 4636
68808f4e 4637 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
d07ceecd
MD
4638 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
4639 /*
4640 * Don't take channel into account if we
4641 * already grab all its packets.
4642 */
4643 continue;
68808f4e 4644 }
d07ceecd
MD
4645 tot_size += chan->channel->attr.subbuf_size
4646 * chan->stream_count;
68808f4e
DG
4647 }
4648 }
4649
4650 if (session->ust_session) {
fb9a95c4 4651 const struct ltt_ust_session *usess = session->ust_session;
68808f4e 4652
d07ceecd
MD
4653 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
4654 cur_nr_packets);
68808f4e
DG
4655 }
4656
d07ceecd 4657 return tot_size;
68808f4e
DG
4658}
4659
5c786ded 4660/*
d07ceecd
MD
4661 * Calculate the number of packets we can grab from each stream that
4662 * fits within the overall snapshot max size.
4663 *
4664 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4665 * the number of packets per stream.
4666 *
4667 * TODO: this approach is not perfect: we consider the worse case
4668 * (packet filling the sub-buffers) as an upper bound, but we could do
4669 * better if we do this calculation while we actually grab the packet
4670 * content: we would know how much padding we don't actually store into
4671 * the file.
4672 *
4673 * This algorithm is currently bounded by the number of packets per
4674 * stream.
4675 *
4676 * Since we call this algorithm before actually grabbing the data, it's
4677 * an approximation: for instance, applications could appear/disappear
4678 * in between this call and actually grabbing data.
5c786ded 4679 */
d07ceecd 4680static
fb9a95c4
JG
4681int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
4682 uint64_t max_size)
5c786ded 4683{
d07ceecd
MD
4684 int64_t size_left;
4685 uint64_t cur_nb_packets = 0;
5c786ded 4686
d07ceecd
MD
4687 if (!max_size) {
4688 return 0; /* Infinite */
5c786ded
JD
4689 }
4690
d07ceecd
MD
4691 size_left = max_size;
4692 for (;;) {
4693 uint64_t one_more_packet_tot_size;
5c786ded 4694
fb9a95c4
JG
4695 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
4696 session, cur_nb_packets);
d07ceecd
MD
4697 if (!one_more_packet_tot_size) {
4698 /* We are already grabbing all packets. */
4699 break;
4700 }
4701 size_left -= one_more_packet_tot_size;
4702 if (size_left < 0) {
4703 break;
4704 }
4705 cur_nb_packets++;
5c786ded 4706 }
aecf2da5 4707 if (!cur_nb_packets && size_left != max_size) {
d07ceecd
MD
4708 /* Not enough room to grab one packet of each stream, error. */
4709 return -1;
4710 }
4711 return cur_nb_packets;
5c786ded
JD
4712}
4713
fb9a95c4 4714static
d2956687 4715enum lttng_error_code snapshot_record(struct ltt_session *session,
fb9a95c4
JG
4716 const struct snapshot_output *snapshot_output, int wait)
4717{
4718 int64_t nb_packets_per_stream;
d2956687 4719 char snapshot_chunk_name[LTTNG_NAME_MAX];
348a81dc
JG
4720 int ret;
4721 enum lttng_error_code ret_code = LTTNG_OK;
d2956687 4722 struct lttng_trace_chunk *snapshot_trace_chunk;
348a81dc
JG
4723 struct consumer_output *original_ust_consumer_output = NULL;
4724 struct consumer_output *original_kernel_consumer_output = NULL;
4725 struct consumer_output *snapshot_ust_consumer_output = NULL;
4726 struct consumer_output *snapshot_kernel_consumer_output = NULL;
d2956687 4727
348a81dc 4728 ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
d2956687
JG
4729 "%s-%s-%" PRIu64,
4730 snapshot_output->name,
4731 snapshot_output->datetime,
4732 snapshot_output->nb_snapshot);
348a81dc 4733 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
d2956687 4734 ERR("Failed to format snapshot name");
348a81dc
JG
4735 ret_code = LTTNG_ERR_INVALID;
4736 goto error;
d2956687
JG
4737 }
4738 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4739 snapshot_output->name, session->name,
4740 snapshot_chunk_name);
348a81dc
JG
4741 if (!session->kernel_session && !session->ust_session) {
4742 ERR("Failed to record snapshot as no channels exist");
4743 ret_code = LTTNG_ERR_NO_CHANNEL;
4744 goto error;
4745 }
4746
4747 if (session->kernel_session) {
4748 original_kernel_consumer_output =
4749 session->kernel_session->consumer;
4750 snapshot_kernel_consumer_output =
4751 consumer_copy_output(snapshot_output->consumer);
3b967712
MD
4752 strcpy(snapshot_kernel_consumer_output->chunk_path,
4753 snapshot_chunk_name);
348a81dc
JG
4754 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
4755 original_kernel_consumer_output);
4756 if (ret < 0) {
4757 ERR("Failed to copy consumer sockets from snapshot output configuration");
4758 ret_code = LTTNG_ERR_NOMEM;
4759 goto error;
4760 }
4761 ret_code = set_relayd_for_snapshot(
4762 snapshot_kernel_consumer_output, session);
4763 if (ret_code != LTTNG_OK) {
4764 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4765 goto error;
4766 }
4767 session->kernel_session->consumer =
4768 snapshot_kernel_consumer_output;
4769 }
4770 if (session->ust_session) {
4771 original_ust_consumer_output = session->ust_session->consumer;
4772 snapshot_ust_consumer_output =
4773 consumer_copy_output(snapshot_output->consumer);
3b967712
MD
4774 strcpy(snapshot_ust_consumer_output->chunk_path,
4775 snapshot_chunk_name);
348a81dc
JG
4776 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
4777 original_ust_consumer_output);
4778 if (ret < 0) {
4779 ERR("Failed to copy consumer sockets from snapshot output configuration");
4780 ret_code = LTTNG_ERR_NOMEM;
4781 goto error;
4782 }
4783 ret_code = set_relayd_for_snapshot(
4784 snapshot_ust_consumer_output, session);
4785 if (ret_code != LTTNG_OK) {
4786 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4787 goto error;
4788 }
4789 session->ust_session->consumer =
4790 snapshot_ust_consumer_output;
4791 }
4792
d2956687 4793 snapshot_trace_chunk = session_create_new_trace_chunk(session,
348a81dc
JG
4794 snapshot_kernel_consumer_output ?:
4795 snapshot_ust_consumer_output,
4796 consumer_output_get_base_path(
4797 snapshot_output->consumer),
d2956687
JG
4798 snapshot_chunk_name);
4799 if (!snapshot_trace_chunk) {
348a81dc
JG
4800 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4801 session->name);
4802 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
4803 goto error;
d2956687
JG
4804 }
4805 assert(!session->current_trace_chunk);
4806 ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
4807 lttng_trace_chunk_put(snapshot_trace_chunk);
4808 snapshot_trace_chunk = NULL;
4809 if (ret) {
348a81dc
JG
4810 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4811 session->name);
4812 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
4813 goto error;
d2956687 4814 }
fb9a95c4
JG
4815
4816 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
4817 snapshot_output->max_size);
4818 if (nb_packets_per_stream < 0) {
348a81dc 4819 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5151d412 4820 goto error_close_trace_chunk;
fb9a95c4
JG
4821 }
4822
4823 if (session->kernel_session) {
348a81dc
JG
4824 ret_code = record_kernel_snapshot(session->kernel_session,
4825 snapshot_kernel_consumer_output, session,
fb9a95c4 4826 wait, nb_packets_per_stream);
348a81dc 4827 if (ret_code != LTTNG_OK) {
5151d412 4828 goto error_close_trace_chunk;
fb9a95c4
JG
4829 }
4830 }
4831
4832 if (session->ust_session) {
348a81dc
JG
4833 ret_code = record_ust_snapshot(session->ust_session,
4834 snapshot_ust_consumer_output, session,
fb9a95c4 4835 wait, nb_packets_per_stream);
348a81dc 4836 if (ret_code != LTTNG_OK) {
5151d412 4837 goto error_close_trace_chunk;
fb9a95c4
JG
4838 }
4839 }
d2956687 4840
5151d412 4841error_close_trace_chunk:
dbfee52c
MD
4842 if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
4843 ERR("Failed to release the current trace chunk of session \"%s\"",
4844 session->name);
4845 ret_code = LTTNG_ERR_UNK;
4846 }
4847
4848 if (session_close_trace_chunk(session, snapshot_trace_chunk,
343defc2 4849 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) {
d2956687
JG
4850 /*
4851 * Don't goto end; make sure the chunk is closed for the session
4852 * to allow future snapshots.
4853 */
4854 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4855 session->name);
348a81dc 4856 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
d2956687 4857 }
348a81dc
JG
4858error:
4859 if (original_ust_consumer_output) {
4860 session->ust_session->consumer = original_ust_consumer_output;
4861 }
4862 if (original_kernel_consumer_output) {
4863 session->kernel_session->consumer =
4864 original_kernel_consumer_output;
4865 }
4866 consumer_output_put(snapshot_ust_consumer_output);
4867 consumer_output_put(snapshot_kernel_consumer_output);
4868 return ret_code;
fb9a95c4
JG
4869}
4870
6dc3064a
DG
4871/*
4872 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4873 *
4874 * The wait parameter is ignored so this call always wait for the snapshot to
4875 * complete before returning.
4876 *
4877 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4878 */
4879int cmd_snapshot_record(struct ltt_session *session,
df4f5a87 4880 const struct lttng_snapshot_output *output, int wait)
6dc3064a 4881{
9a654598
JG
4882 enum lttng_error_code cmd_ret = LTTNG_OK;
4883 int ret;
00e1dfc4 4884 unsigned int snapshot_success = 0;
10ba83fe 4885 char datetime[16];
2abe7969 4886 struct snapshot_output *tmp_output = NULL;
6dc3064a
DG
4887
4888 assert(session);
ba45d9f0 4889 assert(output);
6dc3064a
DG
4890
4891 DBG("Cmd snapshot record for session %s", session->name);
4892
10ba83fe
JR
4893 /* Get the datetime for the snapshot output directory. */
4894 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
4895 sizeof(datetime));
4896 if (!ret) {
9a654598 4897 cmd_ret = LTTNG_ERR_INVALID;
10ba83fe
JR
4898 goto error;
4899 }
4900
6dc3064a 4901 /*
d3f14b8a
MD
4902 * Permission denied to create an output if the session is not
4903 * set in no output mode.
6dc3064a
DG
4904 */
4905 if (session->output_traces) {
9a654598 4906 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
4907 goto error;
4908 }
4909
4910 /* The session needs to be started at least once. */
8382cf6f 4911 if (!session->has_been_started) {
9a654598 4912 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
6dc3064a
DG
4913 goto error;
4914 }
4915
4916 /* Use temporary output for the session. */
ba45d9f0 4917 if (*output->ctrl_url != '\0') {
2abe7969
JG
4918 tmp_output = snapshot_output_alloc();
4919 if (!tmp_output) {
4920 cmd_ret = LTTNG_ERR_NOMEM;
4921 goto error;
4922 }
4923
b178f53e
JG
4924 ret = snapshot_output_init(session, output->max_size,
4925 output->name,
4926 output->ctrl_url, output->data_url,
4927 session->consumer,
2abe7969 4928 tmp_output, NULL);
6dc3064a
DG
4929 if (ret < 0) {
4930 if (ret == -ENOMEM) {
9a654598 4931 cmd_ret = LTTNG_ERR_NOMEM;
6dc3064a 4932 } else {
9a654598 4933 cmd_ret = LTTNG_ERR_INVALID;
6dc3064a
DG
4934 }
4935 goto error;
4936 }
1bfe7328 4937 /* Use the global session count for the temporary snapshot. */
2abe7969 4938 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
10ba83fe
JR
4939
4940 /* Use the global datetime */
2abe7969
JG
4941 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
4942 cmd_ret = snapshot_record(session, tmp_output, wait);
fb9a95c4 4943 if (cmd_ret != LTTNG_OK) {
804c90a8
JR
4944 goto error;
4945 }
804c90a8
JR
4946 snapshot_success = 1;
4947 } else {
4948 struct snapshot_output *sout;
4949 struct lttng_ht_iter iter;
68808f4e 4950
804c90a8
JR
4951 rcu_read_lock();
4952 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
4953 &iter.iter, sout, node.node) {
2abe7969
JG
4954 struct snapshot_output output_copy;
4955
804c90a8 4956 /*
2abe7969
JG
4957 * Make a local copy of the output and override output
4958 * parameters with those provided as part of the
4959 * command.
804c90a8 4960 */
2abe7969 4961 memcpy(&output_copy, sout, sizeof(output_copy));
1bfe7328 4962
804c90a8 4963 if (output->max_size != (uint64_t) -1ULL) {
2abe7969 4964 output_copy.max_size = output->max_size;
6dc3064a 4965 }
d07ceecd 4966
2abe7969
JG
4967 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
4968 memcpy(output_copy.datetime, datetime,
4969 sizeof(datetime));
6dc3064a 4970
804c90a8
JR
4971 /* Use temporary name. */
4972 if (*output->name != '\0') {
2abe7969
JG
4973 if (lttng_strncpy(output_copy.name,
4974 output->name,
4975 sizeof(output_copy.name))) {
9a654598 4976 cmd_ret = LTTNG_ERR_INVALID;
cf3e357d
MD
4977 rcu_read_unlock();
4978 goto error;
4979 }
804c90a8 4980 }
e1986656 4981
2abe7969 4982 cmd_ret = snapshot_record(session, &output_copy, wait);
fb9a95c4
JG
4983 if (cmd_ret != LTTNG_OK) {
4984 rcu_read_unlock();
4985 goto error;
6dc3064a 4986 }
804c90a8 4987 snapshot_success = 1;
6dc3064a 4988 }
804c90a8 4989 rcu_read_unlock();
6dc3064a
DG
4990 }
4991
1bfe7328
DG
4992 if (snapshot_success) {
4993 session->snapshot.nb_snapshot++;
b67578cb 4994 } else {
9a654598 4995 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
4996 }
4997
6dc3064a 4998error:
2abe7969
JG
4999 if (tmp_output) {
5000 snapshot_output_destroy(tmp_output);
5001 }
9a654598 5002 return cmd_ret;
6dc3064a
DG
5003}
5004
d7ba1388
MD
5005/*
5006 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5007 */
5008int cmd_set_session_shm_path(struct ltt_session *session,
5009 const char *shm_path)
5010{
5011 /* Safety net */
5012 assert(session);
5013
5014 /*
5015 * Can only set shm path before session is started.
5016 */
5017 if (session->has_been_started) {
5018 return LTTNG_ERR_SESSION_STARTED;
5019 }
5020
5021 strncpy(session->shm_path, shm_path,
5022 sizeof(session->shm_path));
5023 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5024
5025 return 0;
5026}
5027
5c408ad8
JD
5028/*
5029 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5030 *
5031 * Ask the consumer to rotate the session output directory.
5032 * The session lock must be held.
5033 *
d5a1b7aa 5034 * Returns LTTNG_OK on success or else a negative LTTng error code.
5c408ad8
JD
5035 */
5036int cmd_rotate_session(struct ltt_session *session,
7fdbed1c 5037 struct lttng_rotate_session_return *rotate_return,
343defc2
MD
5038 bool quiet_rotation,
5039 enum lttng_trace_chunk_command_type command)
5c408ad8
JD
5040{
5041 int ret;
d2956687 5042 uint64_t ongoing_rotation_chunk_id;
d5a1b7aa 5043 enum lttng_error_code cmd_ret = LTTNG_OK;
d2956687
JG
5044 struct lttng_trace_chunk *chunk_being_archived = NULL;
5045 struct lttng_trace_chunk *new_trace_chunk = NULL;
5046 enum lttng_trace_chunk_status chunk_status;
3156892b
JG
5047 bool failed_to_rotate = false;
5048 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5c408ad8
JD
5049
5050 assert(session);
5051
5052 if (!session->has_been_started) {
d5a1b7aa 5053 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
d68c9a04 5054 goto end;
5c408ad8
JD
5055 }
5056
d48d65e1
MD
5057 /*
5058 * Explicit rotation is not supported for live sessions.
5059 * However, live sessions can perform a quiet rotation on
5060 * destroy.
5061 * Rotation is not supported for snapshot traces (no output).
5062 */
5063 if ((!quiet_rotation && session->live_timer) ||
5064 !session->output_traces) {
d5a1b7aa 5065 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
d68c9a04 5066 goto end;
5c408ad8
JD
5067 }
5068
d2956687 5069 /* Unsupported feature in lttng-relayd before 2.11. */
070b6a86 5070 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5c408ad8
JD
5071 (session->consumer->relay_major_version == 2 &&
5072 session->consumer->relay_minor_version < 11)) {
d5a1b7aa 5073 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
d68c9a04 5074 goto end;
5c408ad8
JD
5075 }
5076
a40a503f
MD
5077 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5078 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5079 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5080 goto end;
5081 }
5082
92816cc3 5083 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
92816cc3
JG
5084 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5085 session->name);
d5a1b7aa 5086 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
d68c9a04 5087 goto end;
5c408ad8
JD
5088 }
5089
5090 /*
5091 * After a stop, we only allow one rotation to occur, the other ones are
5092 * useless until a new start.
5093 */
5094 if (session->rotated_after_last_stop) {
5095 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5096 session->name);
d5a1b7aa 5097 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
d68c9a04 5098 goto end;
5c408ad8 5099 }
b02f5986
MD
5100
5101 /*
5102 * After a stop followed by a clear, disallow following rotations a they would
5103 * generate empty chunks.
5104 */
5105 if (session->cleared_after_last_stop) {
5106 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5107 session->name);
5108 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5109 goto end;
5110 }
5111
d2956687 5112 if (session->active) {
348a81dc 5113 new_trace_chunk = session_create_new_trace_chunk(session, NULL,
d2956687
JG
5114 NULL, NULL);
5115 if (!new_trace_chunk) {
5116 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5117 goto error;
5c408ad8 5118 }
0e270a1e 5119 }
2961f09e 5120
3156892b
JG
5121 /*
5122 * The current trace chunk becomes the chunk being archived.
5123 *
5124 * After this point, "chunk_being_archived" must absolutely
5125 * be closed on the consumer(s), otherwise it will never be
5126 * cleaned-up, which will result in a leak.
5127 */
d2956687
JG
5128 ret = session_set_trace_chunk(session, new_trace_chunk,
5129 &chunk_being_archived);
5130 if (ret) {
5131 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
b178f53e
JG
5132 goto error;
5133 }
5134
5c408ad8 5135 if (session->kernel_session) {
d5a1b7aa
JG
5136 cmd_ret = kernel_rotate_session(session);
5137 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5138 failed_to_rotate = true;
5139 rotation_fail_code = cmd_ret;
5c408ad8
JD
5140 }
5141 }
5142 if (session->ust_session) {
d5a1b7aa
JG
5143 cmd_ret = ust_app_rotate_session(session);
5144 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5145 failed_to_rotate = true;
5146 rotation_fail_code = cmd_ret;
5c408ad8 5147 }
92816cc3 5148 }
17dd1232 5149
3b61d9ee
JG
5150 if (!session->active) {
5151 session->rotated_after_last_stop = true;
5152 }
5153
5154 if (!chunk_being_archived) {
5155 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5156 session->name);
5157 if (failed_to_rotate) {
5158 cmd_ret = rotation_fail_code;
5159 goto error;
5160 }
5161 cmd_ret = LTTNG_OK;
5162 goto end;
5163 }
5164
5165 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5166 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
5167 &ongoing_rotation_chunk_id);
5168 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5169
bbc4768c 5170 ret = session_close_trace_chunk(session, chunk_being_archived,
343defc2 5171 command, session->last_chunk_path);
d2956687
JG
5172 if (ret) {
5173 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5174 goto error;
5175 }
5176
3156892b
JG
5177 if (failed_to_rotate) {
5178 cmd_ret = rotation_fail_code;
5179 goto error;
5180 }
5181
7fdbed1c 5182 session->quiet_rotation = quiet_rotation;
92816cc3
JG
5183 ret = timer_session_rotation_pending_check_start(session,
5184 DEFAULT_ROTATE_PENDING_TIMER);
5185 if (ret) {
d5a1b7aa 5186 cmd_ret = LTTNG_ERR_UNK;
2961f09e 5187 goto error;
5c408ad8
JD
5188 }
5189
5c408ad8 5190 if (rotate_return) {
d2956687 5191 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5c408ad8
JD
5192 }
5193
d2956687
JG
5194 session->chunk_being_archived = chunk_being_archived;
5195 chunk_being_archived = NULL;
7fdbed1c
JG
5196 if (!quiet_rotation) {
5197 ret = notification_thread_command_session_rotation_ongoing(
5198 notification_thread_handle,
5199 session->name, session->uid, session->gid,
5200 ongoing_rotation_chunk_id);
5201 if (ret != LTTNG_OK) {
5202 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5203 session->name);
5204 cmd_ret = ret;
5205 }
2961f09e
JG
5206 }
5207
92816cc3 5208 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
d2956687 5209 session->name, ongoing_rotation_chunk_id);
5c408ad8 5210end:
d2956687
JG
5211 lttng_trace_chunk_put(new_trace_chunk);
5212 lttng_trace_chunk_put(chunk_being_archived);
d5a1b7aa 5213 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5c408ad8 5214 return ret;
2961f09e 5215error:
2961f09e 5216 if (session_reset_rotation_state(session,
d2956687 5217 LTTNG_ROTATION_STATE_ERROR)) {
2961f09e
JG
5218 ERR("Failed to reset rotation state of session \"%s\"",
5219 session->name);
5220 }
5221 goto end;
5c408ad8
JD
5222}
5223
5224/*
d68c9a04 5225 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5c408ad8
JD
5226 *
5227 * Check if the session has finished its rotation.
5228 *
d2956687 5229 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5c408ad8 5230 */
d68c9a04
JD
5231int cmd_rotate_get_info(struct ltt_session *session,
5232 struct lttng_rotation_get_info_return *info_return,
5233 uint64_t rotation_id)
5c408ad8 5234{
d2956687
JG
5235 enum lttng_error_code cmd_ret = LTTNG_OK;
5236 enum lttng_rotation_state rotation_state;
5c408ad8 5237
d68c9a04 5238 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
d2956687 5239 session->most_recent_chunk_id.value);
5c408ad8 5240
d2956687
JG
5241 if (session->chunk_being_archived) {
5242 enum lttng_trace_chunk_status chunk_status;
5243 uint64_t chunk_id;
5244
5245 chunk_status = lttng_trace_chunk_get_id(
5246 session->chunk_being_archived,
5247 &chunk_id);
5248 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5249
5250 rotation_state = rotation_id == chunk_id ?
5251 LTTNG_ROTATION_STATE_ONGOING :
5252 LTTNG_ROTATION_STATE_EXPIRED;
5253 } else {
5254 if (session->last_archived_chunk_id.is_set &&
5255 rotation_id != session->last_archived_chunk_id.value) {
5256 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5257 } else {
5258 rotation_state = session->rotation_state;
5259 }
5c408ad8
JD
5260 }
5261
d2956687
JG
5262 switch (rotation_state) {
5263 case LTTNG_ROTATION_STATE_NO_ROTATION:
83ed9e90 5264 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
d2956687
JG
5265 session->name);
5266 goto end;
5267 case LTTNG_ROTATION_STATE_EXPIRED:
5268 DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
5269 rotation_id, session->name);
5270 break;
d68c9a04 5271 case LTTNG_ROTATION_STATE_ONGOING:
d2956687 5272 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
d68c9a04
JD
5273 rotation_id, session->name);
5274 break;
5275 case LTTNG_ROTATION_STATE_COMPLETED:
dd73d57b 5276 {
d2956687
JG
5277 int fmt_ret;
5278 char *chunk_path;
dd73d57b
JG
5279 char *current_tracing_path_reply;
5280 size_t current_tracing_path_reply_len;
5281
d2956687
JG
5282 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5283 rotation_id, session->name);
5284
dd73d57b
JG
5285 switch (session_get_consumer_destination_type(session)) {
5286 case CONSUMER_DST_LOCAL:
5287 current_tracing_path_reply =
5288 info_return->location.local.absolute_path;
5289 current_tracing_path_reply_len =
5290 sizeof(info_return->location.local.absolute_path);
5291 info_return->location_type =
05f8afa9 5292 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
ecd1a12f
MD
5293 fmt_ret = asprintf(&chunk_path,
5294 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5295 session_get_base_path(session),
5296 session->last_archived_chunk_name);
5297 if (fmt_ret == -1) {
5298 PERROR("Failed to format the path of the last archived trace chunk");
5299 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5300 cmd_ret = LTTNG_ERR_UNK;
5301 goto end;
5302 }
dd73d57b
JG
5303 break;
5304 case CONSUMER_DST_NET:
09cfbe47
JG
5305 {
5306 uint16_t ctrl_port, data_port;
5307
dd73d57b
JG
5308 current_tracing_path_reply =
5309 info_return->location.relay.relative_path;
5310 current_tracing_path_reply_len =
5311 sizeof(info_return->location.relay.relative_path);
5312 /* Currently the only supported relay protocol. */
5313 info_return->location.relay.protocol =
05f8afa9 5314 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
dd73d57b 5315
d2956687 5316 fmt_ret = lttng_strncpy(info_return->location.relay.host,
dd73d57b
JG
5317 session_get_net_consumer_hostname(session),
5318 sizeof(info_return->location.relay.host));
d2956687
JG
5319 if (fmt_ret) {
5320 ERR("Failed to copy host name to rotate_get_info reply");
dd73d57b 5321 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5322 cmd_ret = LTTNG_ERR_SET_URL;
dd73d57b
JG
5323 goto end;
5324 }
5325
09cfbe47
JG
5326 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5327 info_return->location.relay.ports.control = ctrl_port;
5328 info_return->location.relay.ports.data = data_port;
dd73d57b 5329 info_return->location_type =
05f8afa9 5330 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
ecd1a12f
MD
5331 chunk_path = strdup(session->last_chunk_path);
5332 if (!chunk_path) {
5333 ERR("Failed to allocate the path of the last archived trace chunk");
5334 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5335 cmd_ret = LTTNG_ERR_UNK;
5336 goto end;
5337 }
dd73d57b 5338 break;
09cfbe47 5339 }
dd73d57b
JG
5340 default:
5341 abort();
5342 }
d2956687
JG
5343
5344 fmt_ret = lttng_strncpy(current_tracing_path_reply,
5345 chunk_path, current_tracing_path_reply_len);
5346 free(chunk_path);
5347 if (fmt_ret) {
5348 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
d68c9a04 5349 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5350 cmd_ret = LTTNG_ERR_UNK;
5c408ad8
JD
5351 goto end;
5352 }
dd73d57b 5353
d68c9a04 5354 break;
dd73d57b 5355 }
d68c9a04 5356 case LTTNG_ROTATION_STATE_ERROR:
d2956687 5357 DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
d68c9a04
JD
5358 rotation_id, session->name);
5359 break;
5360 default:
5361 abort();
5c408ad8
JD
5362 }
5363
d2956687 5364 cmd_ret = LTTNG_OK;
5c408ad8 5365end:
d2956687
JG
5366 info_return->status = (int32_t) rotation_state;
5367 return cmd_ret;
5c408ad8
JD
5368}
5369
259c2674
JD
5370/*
5371 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5372 *
5373 * Configure the automatic rotation parameters.
66ea93b1
JG
5374 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5375 * 'activate' to false means deactivate the rotation schedule and validate that
5376 * 'new_value' has the same value as the currently active value.
259c2674 5377 *
66ea93b1 5378 * Return 0 on success or else a positive LTTNG_ERR code.
259c2674
JD
5379 */
5380int cmd_rotation_set_schedule(struct ltt_session *session,
66ea93b1
JG
5381 bool activate, enum lttng_rotation_schedule_type schedule_type,
5382 uint64_t new_value,
90936dcf 5383 struct notification_thread_handle *notification_thread_handle)
259c2674
JD
5384{
5385 int ret;
66ea93b1 5386 uint64_t *parameter_value;
259c2674
JD
5387
5388 assert(session);
5389
5390 DBG("Cmd rotate set schedule session %s", session->name);
5391
92fe5ca1 5392 if (session->live_timer || !session->output_traces) {
66ea93b1 5393 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
259c2674
JD
5394 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5395 goto end;
5396 }
5397
66ea93b1
JG
5398 switch (schedule_type) {
5399 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5400 parameter_value = &session->rotate_size;
5401 break;
5402 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5403 parameter_value = &session->rotate_timer_period;
5404 if (new_value >= UINT_MAX) {
5405 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
5406 new_value, UINT_MAX);
5407 ret = LTTNG_ERR_INVALID;
5408 goto end;
5409 }
5410 break;
5411 default:
5412 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5413 ret = LTTNG_ERR_INVALID;
259c2674 5414 goto end;
66ea93b1
JG
5415 }
5416
5417 /* Improper use of the API. */
5418 if (new_value == -1ULL) {
5419 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5420 ret = LTTNG_ERR_INVALID;
259c2674
JD
5421 goto end;
5422 }
5423
66ea93b1
JG
5424 /*
5425 * As indicated in struct ltt_session's comments, a value of == 0 means
5426 * this schedule rotation type is not in use.
5427 *
5428 * Reject the command if we were asked to activate a schedule that was
5429 * already active.
5430 */
5431 if (activate && *parameter_value != 0) {
5432 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5433 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
90936dcf 5434 goto end;
66ea93b1
JG
5435 }
5436
5437 /*
5438 * Reject the command if we were asked to deactivate a schedule that was
5439 * not active.
5440 */
5441 if (!activate && *parameter_value == 0) {
5442 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5443 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
90936dcf
JD
5444 goto end;
5445 }
5446
66ea93b1
JG
5447 /*
5448 * Reject the command if we were asked to deactivate a schedule that
5449 * doesn't exist.
5450 */
5451 if (!activate && *parameter_value != new_value) {
5452 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5453 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5454 goto end;
5455 }
259c2674 5456
66ea93b1
JG
5457 *parameter_value = activate ? new_value : 0;
5458
5459 switch (schedule_type) {
5460 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5461 if (activate && session->active) {
5462 /*
5463 * Only start the timer if the session is active,
5464 * otherwise it will be started when the session starts.
5465 */
92816cc3
JG
5466 ret = timer_session_rotation_schedule_timer_start(
5467 session, new_value);
259c2674 5468 if (ret) {
66ea93b1 5469 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
259c2674
JD
5470 ret = LTTNG_ERR_UNK;
5471 goto end;
5472 }
66ea93b1 5473 } else {
92816cc3
JG
5474 ret = timer_session_rotation_schedule_timer_stop(
5475 session);
66ea93b1
JG
5476 if (ret) {
5477 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5478 ret = LTTNG_ERR_UNK;
f3ce6946 5479 goto end;
66ea93b1 5480 }
259c2674 5481 }
66ea93b1
JG
5482 break;
5483 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5484 if (activate) {
5485 ret = subscribe_session_consumed_size_rotation(session,
5486 new_value, notification_thread_handle);
90936dcf 5487 if (ret) {
66ea93b1 5488 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
90936dcf
JD
5489 ret = LTTNG_ERR_UNK;
5490 goto end;
5491 }
90936dcf 5492 } else {
66ea93b1
JG
5493 ret = unsubscribe_session_consumed_size_rotation(session,
5494 notification_thread_handle);
90936dcf 5495 if (ret) {
66ea93b1 5496 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
90936dcf
JD
5497 ret = LTTNG_ERR_UNK;
5498 goto end;
5499 }
66ea93b1 5500
90936dcf 5501 }
66ea93b1
JG
5502 break;
5503 default:
5504 /* Would have been caught before. */
5505 abort();
90936dcf
JD
5506 }
5507
259c2674
JD
5508 ret = LTTNG_OK;
5509
5510 goto end;
5511
5512end:
5513 return ret;
5514}
5515
a503e1ef
JG
5516/* Wait for a given path to be removed before continuing. */
5517static enum lttng_error_code wait_on_path(void *path_data)
5518{
5519 const char *shm_path = path_data;
5520
5521 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5522 shm_path);
5523 while (true) {
5524 int ret;
5525 struct stat st;
5526
5527 ret = stat(shm_path, &st);
5528 if (ret) {
5529 if (errno != ENOENT) {
5530 PERROR("stat() returned an error while checking for the existence of the shm path");
5531 } else {
5532 DBG("shm path no longer exists, completing the destruction of session");
5533 }
5534 break;
5535 } else {
5536 if (!S_ISDIR(st.st_mode)) {
5537 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5538 shm_path);
5539 break;
5540 }
5541 }
5542 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5543 }
5544 return LTTNG_OK;
5545}
5546
5547/*
5548 * Returns a pointer to a handler to run on completion of a command.
5549 * Returns NULL if no handler has to be run for the last command executed.
5550 */
5551const struct cmd_completion_handler *cmd_pop_completion_handler(void)
5552{
5553 struct cmd_completion_handler *handler = current_completion_handler;
5554
5555 current_completion_handler = NULL;
5556 return handler;
5557}
5558
2f77fc4b
DG
5559/*
5560 * Init command subsystem.
5561 */
5562void cmd_init(void)
5563{
5564 /*
d88aee68
DG
5565 * Set network sequence index to 1 for streams to match a relayd
5566 * socket on the consumer side.
2f77fc4b 5567 */
d88aee68
DG
5568 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5569 relayd_net_seq_idx = 1;
5570 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
5571
5572 DBG("Command subsystem initialized");
5573}
This page took 0.403418 seconds and 4 git commands to generate.