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