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