Fix: Send remove channel to notification thread only when necessary
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
CommitLineData
2f77fc4b
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
2f77fc4b
DG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
6c1c0768 19#define _LGPL_SOURCE
2f77fc4b 20#include <assert.h>
6dc3064a 21#include <inttypes.h>
2f77fc4b
DG
22#include <urcu/list.h>
23#include <urcu/uatomic.h>
24
25#include <common/defaults.h>
26#include <common/common.h>
27#include <common/sessiond-comm/sessiond-comm.h>
28#include <common/relayd/relayd.h>
10a50311 29#include <common/utils.h>
f5436bfc 30#include <common/compat/string.h>
93ec662e 31#include <common/kernel-ctl/kernel-ctl.h>
b0880ae5
JG
32#include <common/dynamic-buffer.h>
33#include <common/buffer-view.h>
34#include <lttng/trigger/trigger-internal.h>
35#include <lttng/condition/condition.h>
36#include <lttng/action/action.h>
37#include <lttng/channel-internal.h>
9f449915 38#include <common/string-utils/string-utils.h>
2f77fc4b
DG
39
40#include "channel.h"
41#include "consumer.h"
42#include "event.h"
8782cc74 43#include "health-sessiond.h"
2f77fc4b
DG
44#include "kernel.h"
45#include "kernel-consumer.h"
46#include "lttng-sessiond.h"
47#include "utils.h"
834978fd 48#include "syscall.h"
7c1d2758 49#include "agent.h"
93ec662e 50#include "buffer-registry.h"
b0880ae5
JG
51#include "notification-thread.h"
52#include "notification-thread-commands.h"
2f77fc4b
DG
53
54#include "cmd.h"
55
56/*
57 * Used to keep a unique index for each relayd socket created where this value
58 * is associated with streams on the consumer so it can match the right relayd
d88aee68
DG
59 * to send to. It must be accessed with the relayd_net_seq_idx_lock
60 * held.
2f77fc4b 61 */
d88aee68
DG
62static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
63static uint64_t relayd_net_seq_idx;
2f77fc4b 64
7076b56e
JG
65static int validate_ust_event_name(const char *);
66static int cmd_enable_event_internal(struct ltt_session *session,
67 struct lttng_domain *domain,
68 char *channel_name, struct lttng_event *event,
69 char *filter_expression,
70 struct lttng_filter_bytecode *filter,
71 struct lttng_event_exclusion *exclusion,
72 int wpipe);
73
2f77fc4b
DG
74/*
75 * Create a session path used by list_lttng_sessions for the case that the
76 * session consumer is on the network.
77 */
78static int build_network_session_path(char *dst, size_t size,
79 struct ltt_session *session)
80{
81 int ret, kdata_port, udata_port;
82 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
83 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
84
85 assert(session);
86 assert(dst);
87
88 memset(tmp_urls, 0, sizeof(tmp_urls));
89 memset(tmp_uurl, 0, sizeof(tmp_uurl));
90
91 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
92
93 if (session->kernel_session && session->kernel_session->consumer) {
94 kuri = &session->kernel_session->consumer->dst.net.control;
95 kdata_port = session->kernel_session->consumer->dst.net.data.port;
96 }
97
98 if (session->ust_session && session->ust_session->consumer) {
99 uuri = &session->ust_session->consumer->dst.net.control;
100 udata_port = session->ust_session->consumer->dst.net.data.port;
101 }
102
103 if (uuri == NULL && kuri == NULL) {
104 uri = &session->consumer->dst.net.control;
105 kdata_port = session->consumer->dst.net.data.port;
106 } else if (kuri && uuri) {
107 ret = uri_compare(kuri, uuri);
108 if (ret) {
109 /* Not Equal */
110 uri = kuri;
111 /* Build uuri URL string */
112 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
113 if (ret < 0) {
114 goto error;
115 }
116 } else {
117 uri = kuri;
118 }
119 } else if (kuri && uuri == NULL) {
120 uri = kuri;
121 } else if (uuri && kuri == NULL) {
122 uri = uuri;
123 }
124
125 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
126 if (ret < 0) {
127 goto error;
128 }
129
9aa9f900
DG
130 /*
131 * Do we have a UST url set. If yes, this means we have both kernel and UST
132 * to print.
133 */
9d035200 134 if (*tmp_uurl != '\0') {
2f77fc4b
DG
135 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
136 tmp_urls, kdata_port, tmp_uurl, udata_port);
137 } else {
9aa9f900 138 int dport;
bef08707 139 if (kuri || (!kuri && !uuri)) {
9aa9f900
DG
140 dport = kdata_port;
141 } else {
142 /* No kernel URI, use the UST port. */
143 dport = udata_port;
144 }
145 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
2f77fc4b
DG
146 }
147
148error:
149 return ret;
150}
151
fb83fe64
JD
152/*
153 * Get run-time attributes if the session has been started (discarded events,
154 * lost packets).
155 */
156static int get_kernel_runtime_stats(struct ltt_session *session,
157 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
158 uint64_t *lost_packets)
159{
160 int ret;
161
162 if (!session->has_been_started) {
163 ret = 0;
164 *discarded_events = 0;
165 *lost_packets = 0;
166 goto end;
167 }
168
169 ret = consumer_get_discarded_events(session->id, kchan->fd,
170 session->kernel_session->consumer,
171 discarded_events);
172 if (ret < 0) {
173 goto end;
174 }
175
176 ret = consumer_get_lost_packets(session->id, kchan->fd,
177 session->kernel_session->consumer,
178 lost_packets);
179 if (ret < 0) {
180 goto end;
181 }
182
183end:
184 return ret;
185}
186
187/*
188 * Get run-time attributes if the session has been started (discarded events,
189 * lost packets).
190 */
191static int get_ust_runtime_stats(struct ltt_session *session,
192 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
193 uint64_t *lost_packets)
194{
195 int ret;
196 struct ltt_ust_session *usess;
197
a91c5803
JG
198 if (!discarded_events || !lost_packets) {
199 ret = -1;
200 goto end;
201 }
202
fb83fe64 203 usess = session->ust_session;
a905c624
JG
204 assert(discarded_events);
205 assert(lost_packets);
fb83fe64
JD
206
207 if (!usess || !session->has_been_started) {
208 *discarded_events = 0;
209 *lost_packets = 0;
210 ret = 0;
211 goto end;
212 }
213
214 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
215 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
216 &usess->buffer_reg_uid_list,
217 usess->consumer, uchan->id,
218 uchan->attr.overwrite,
219 discarded_events,
220 lost_packets);
221 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
222 ret = ust_app_pid_get_channel_runtime_stats(usess,
223 uchan, usess->consumer,
224 uchan->attr.overwrite,
225 discarded_events,
226 lost_packets);
227 if (ret < 0) {
228 goto end;
229 }
230 *discarded_events += uchan->per_pid_closed_app_discarded;
231 *lost_packets += uchan->per_pid_closed_app_lost;
232 } else {
233 ERR("Unsupported buffer type");
967bc289 234 assert(0);
fb83fe64
JD
235 ret = -1;
236 goto end;
237 }
238
239end:
240 return ret;
241}
242
2f77fc4b
DG
243/*
244 * Fill lttng_channel array of all channels.
245 */
d449df4a 246static ssize_t list_lttng_channels(enum lttng_domain_type domain,
fb83fe64 247 struct ltt_session *session, struct lttng_channel *channels,
cf0bcb51 248 struct lttng_channel_extended *chan_exts)
2f77fc4b 249{
d449df4a 250 int i = 0, ret = 0;
2f77fc4b
DG
251 struct ltt_kernel_channel *kchan;
252
253 DBG("Listing channels for session %s", session->name);
254
255 switch (domain) {
256 case LTTNG_DOMAIN_KERNEL:
257 /* Kernel channels */
258 if (session->kernel_session != NULL) {
259 cds_list_for_each_entry(kchan,
260 &session->kernel_session->channel_list.head, list) {
fb83fe64 261 uint64_t discarded_events, lost_packets;
cf0bcb51
JG
262 struct lttng_channel_extended *extended;
263
264 extended = (struct lttng_channel_extended *)
265 kchan->channel->attr.extended.ptr;
fb83fe64
JD
266
267 ret = get_kernel_runtime_stats(session, kchan,
268 &discarded_events, &lost_packets);
269 if (ret < 0) {
270 goto end;
271 }
2f77fc4b
DG
272 /* Copy lttng_channel struct to array */
273 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
274 channels[i].enabled = kchan->enabled;
fb83fe64
JD
275 chan_exts[i].discarded_events =
276 discarded_events;
277 chan_exts[i].lost_packets = lost_packets;
cf0bcb51
JG
278 chan_exts[i].monitor_timer_interval =
279 extended->monitor_timer_interval;
491d1539 280 chan_exts[i].blocking_timeout = 0;
2f77fc4b
DG
281 i++;
282 }
283 }
284 break;
285 case LTTNG_DOMAIN_UST:
286 {
287 struct lttng_ht_iter iter;
288 struct ltt_ust_channel *uchan;
289
e7fe706f 290 rcu_read_lock();
2f77fc4b
DG
291 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
292 &iter.iter, uchan, node.node) {
a91c5803 293 uint64_t discarded_events = 0, lost_packets = 0;
fb83fe64 294
8ee609c8
MD
295 if (lttng_strncpy(channels[i].name, uchan->name,
296 LTTNG_SYMBOL_NAME_LEN)) {
297 break;
298 }
2f77fc4b
DG
299 channels[i].attr.overwrite = uchan->attr.overwrite;
300 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
301 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
302 channels[i].attr.switch_timer_interval =
303 uchan->attr.switch_timer_interval;
304 channels[i].attr.read_timer_interval =
305 uchan->attr.read_timer_interval;
306 channels[i].enabled = uchan->enabled;
2f785fe7
DG
307 channels[i].attr.tracefile_size = uchan->tracefile_size;
308 channels[i].attr.tracefile_count = uchan->tracefile_count;
5e4435a4
JG
309
310 /*
311 * Map enum lttng_ust_output to enum lttng_event_output.
312 */
2f77fc4b
DG
313 switch (uchan->attr.output) {
314 case LTTNG_UST_MMAP:
2f77fc4b
DG
315 channels[i].attr.output = LTTNG_EVENT_MMAP;
316 break;
5e4435a4
JG
317 default:
318 /*
319 * LTTNG_UST_MMAP is the only supported UST
320 * output mode.
321 */
322 assert(0);
323 break;
2f77fc4b 324 }
fb83fe64 325
d449df4a
MD
326 chan_exts[i].monitor_timer_interval =
327 uchan->monitor_timer_interval;
491d1539
MD
328 chan_exts[i].blocking_timeout =
329 uchan->attr.u.s.blocking_timeout;
d449df4a 330
fb83fe64
JD
331 ret = get_ust_runtime_stats(session, uchan,
332 &discarded_events, &lost_packets);
333 if (ret < 0) {
334 break;
335 }
336 chan_exts[i].discarded_events = discarded_events;
337 chan_exts[i].lost_packets = lost_packets;
2f77fc4b
DG
338 i++;
339 }
e7fe706f 340 rcu_read_unlock();
2f77fc4b
DG
341 break;
342 }
343 default:
344 break;
345 }
fb83fe64
JD
346
347end:
d449df4a
MD
348 if (ret < 0) {
349 return -LTTNG_ERR_FATAL;
350 } else {
351 return LTTNG_OK;
352 }
2f77fc4b
DG
353}
354
b4e3ceb9 355static void increment_extended_len(const char *filter_expression,
795d57ce 356 struct lttng_event_exclusion *exclusion, size_t *extended_len)
b4e3ceb9
PP
357{
358 *extended_len += sizeof(struct lttcomm_event_extended_header);
359
360 if (filter_expression) {
361 *extended_len += strlen(filter_expression) + 1;
362 }
795d57ce
PP
363
364 if (exclusion) {
365 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
366 }
b4e3ceb9
PP
367}
368
369static void append_extended_info(const char *filter_expression,
795d57ce 370 struct lttng_event_exclusion *exclusion, void **extended_at)
b4e3ceb9
PP
371{
372 struct lttcomm_event_extended_header extended_header;
373 size_t filter_len = 0;
795d57ce 374 size_t nb_exclusions = 0;
b4e3ceb9
PP
375
376 if (filter_expression) {
377 filter_len = strlen(filter_expression) + 1;
378 }
379
795d57ce
PP
380 if (exclusion) {
381 nb_exclusions = exclusion->count;
382 }
383
384 /* Set header fields */
b4e3ceb9 385 extended_header.filter_len = filter_len;
795d57ce
PP
386 extended_header.nb_exclusions = nb_exclusions;
387
388 /* Copy header */
b4e3ceb9
PP
389 memcpy(*extended_at, &extended_header, sizeof(extended_header));
390 *extended_at += sizeof(extended_header);
795d57ce
PP
391
392 /* Copy filter string */
393 if (filter_expression) {
394 memcpy(*extended_at, filter_expression, filter_len);
395 *extended_at += filter_len;
396 }
397
398 /* Copy exclusion names */
399 if (exclusion) {
400 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
401
402 memcpy(*extended_at, &exclusion->names, len);
403 *extended_at += len;
404 }
b4e3ceb9
PP
405}
406
3c6a091f 407/*
022d91ba 408 * Create a list of agent domain events.
3c6a091f
DG
409 *
410 * Return number of events in list on success or else a negative value.
411 */
022d91ba 412static int list_lttng_agent_events(struct agent *agt,
b4e3ceb9 413 struct lttng_event **events, size_t *total_size)
3c6a091f
DG
414{
415 int i = 0, ret = 0;
416 unsigned int nb_event = 0;
022d91ba 417 struct agent_event *event;
3c6a091f
DG
418 struct lttng_event *tmp_events;
419 struct lttng_ht_iter iter;
b4e3ceb9
PP
420 size_t extended_len = 0;
421 void *extended_at;
3c6a091f 422
022d91ba 423 assert(agt);
3c6a091f
DG
424 assert(events);
425
022d91ba 426 DBG3("Listing agent events");
3c6a091f 427
e5bbf678 428 rcu_read_lock();
022d91ba 429 nb_event = lttng_ht_get_count(agt->events);
e5bbf678 430 rcu_read_unlock();
3c6a091f
DG
431 if (nb_event == 0) {
432 ret = nb_event;
b4e3ceb9 433 *total_size = 0;
3c6a091f
DG
434 goto error;
435 }
436
b4e3ceb9
PP
437 /* Compute required extended infos size */
438 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
439
440 /*
441 * This is only valid because the commands which add events are
442 * processed in the same thread as the listing.
443 */
444 rcu_read_lock();
445 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
795d57ce
PP
446 increment_extended_len(event->filter_expression, NULL,
447 &extended_len);
b4e3ceb9
PP
448 }
449 rcu_read_unlock();
450
451 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
452 tmp_events = zmalloc(*total_size);
3c6a091f 453 if (!tmp_events) {
022d91ba 454 PERROR("zmalloc agent events session");
3c6a091f
DG
455 ret = -LTTNG_ERR_FATAL;
456 goto error;
457 }
458
b4e3ceb9
PP
459 extended_at = ((uint8_t *) tmp_events) +
460 nb_event * sizeof(struct lttng_event);
461
3c6a091f 462 rcu_read_lock();
022d91ba 463 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
3c6a091f
DG
464 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
465 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
466 tmp_events[i].enabled = event->enabled;
2106efa0 467 tmp_events[i].loglevel = event->loglevel_value;
ff94328f 468 tmp_events[i].loglevel_type = event->loglevel_type;
3c6a091f 469 i++;
b4e3ceb9
PP
470
471 /* Append extended info */
795d57ce
PP
472 append_extended_info(event->filter_expression, NULL,
473 &extended_at);
3c6a091f
DG
474 }
475 rcu_read_unlock();
476
477 *events = tmp_events;
478 ret = nb_event;
479
480error:
481 assert(nb_event == i);
482 return ret;
483}
484
2f77fc4b
DG
485/*
486 * Create a list of ust global domain events.
487 */
488static int list_lttng_ust_global_events(char *channel_name,
b4e3ceb9
PP
489 struct ltt_ust_domain_global *ust_global,
490 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
491{
492 int i = 0, ret = 0;
493 unsigned int nb_event = 0;
494 struct lttng_ht_iter iter;
495 struct lttng_ht_node_str *node;
496 struct ltt_ust_channel *uchan;
497 struct ltt_ust_event *uevent;
498 struct lttng_event *tmp;
b4e3ceb9
PP
499 size_t extended_len = 0;
500 void *extended_at;
2f77fc4b
DG
501
502 DBG("Listing UST global events for channel %s", channel_name);
503
504 rcu_read_lock();
505
506 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
507 node = lttng_ht_iter_get_node_str(&iter);
508 if (node == NULL) {
f73fabfd 509 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
d31d3e8c 510 goto end;
2f77fc4b
DG
511 }
512
513 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
514
3c442be3 515 nb_event = lttng_ht_get_count(uchan->events);
2f77fc4b
DG
516 if (nb_event == 0) {
517 ret = nb_event;
b4e3ceb9 518 *total_size = 0;
d31d3e8c 519 goto end;
2f77fc4b
DG
520 }
521
522 DBG3("Listing UST global %d events", nb_event);
523
b4e3ceb9
PP
524 /* Compute required extended infos size */
525 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
526 if (uevent->internal) {
527 nb_event--;
528 continue;
529 }
530
531 increment_extended_len(uevent->filter_expression,
795d57ce 532 uevent->exclusion, &extended_len);
b4e3ceb9 533 }
d31d3e8c
PP
534 if (nb_event == 0) {
535 /* All events are internal, skip. */
536 ret = 0;
537 *total_size = 0;
538 goto end;
539 }
b4e3ceb9
PP
540
541 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
542 tmp = zmalloc(*total_size);
2f77fc4b 543 if (tmp == NULL) {
b12c38df
JG
544 ret = -LTTNG_ERR_FATAL;
545 goto end;
2f77fc4b
DG
546 }
547
b4e3ceb9
PP
548 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
549
2f77fc4b 550 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
3c442be3
JG
551 if (uevent->internal) {
552 /* This event should remain hidden from clients */
3c442be3
JG
553 continue;
554 }
2f77fc4b
DG
555 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
556 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
557 tmp[i].enabled = uevent->enabled;
558
559 switch (uevent->attr.instrumentation) {
560 case LTTNG_UST_TRACEPOINT:
561 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
562 break;
563 case LTTNG_UST_PROBE:
564 tmp[i].type = LTTNG_EVENT_PROBE;
565 break;
566 case LTTNG_UST_FUNCTION:
567 tmp[i].type = LTTNG_EVENT_FUNCTION;
568 break;
569 }
570
571 tmp[i].loglevel = uevent->attr.loglevel;
572 switch (uevent->attr.loglevel_type) {
573 case LTTNG_UST_LOGLEVEL_ALL:
574 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
575 break;
576 case LTTNG_UST_LOGLEVEL_RANGE:
577 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
578 break;
579 case LTTNG_UST_LOGLEVEL_SINGLE:
580 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
581 break;
582 }
583 if (uevent->filter) {
584 tmp[i].filter = 1;
585 }
4634f12e
JI
586 if (uevent->exclusion) {
587 tmp[i].exclusion = 1;
588 }
2f77fc4b 589 i++;
b4e3ceb9
PP
590
591 /* Append extended info */
795d57ce
PP
592 append_extended_info(uevent->filter_expression,
593 uevent->exclusion, &extended_at);
2f77fc4b
DG
594 }
595
596 ret = nb_event;
597 *events = tmp;
d31d3e8c 598end:
2f77fc4b
DG
599 rcu_read_unlock();
600 return ret;
601}
602
603/*
604 * Fill lttng_event array of all kernel events in the channel.
605 */
606static int list_lttng_kernel_events(char *channel_name,
b4e3ceb9
PP
607 struct ltt_kernel_session *kernel_session,
608 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
609{
610 int i = 0, ret;
611 unsigned int nb_event;
612 struct ltt_kernel_event *event;
613 struct ltt_kernel_channel *kchan;
b4e3ceb9
PP
614 size_t extended_len = 0;
615 void *extended_at;
2f77fc4b
DG
616
617 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
618 if (kchan == NULL) {
f73fabfd 619 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
620 goto error;
621 }
622
623 nb_event = kchan->event_count;
624
625 DBG("Listing events for channel %s", kchan->channel->name);
626
627 if (nb_event == 0) {
b4e3ceb9 628 *total_size = 0;
834978fd 629 *events = NULL;
db906c12 630 goto end;
2f77fc4b
DG
631 }
632
b4e3ceb9
PP
633 /* Compute required extended infos size */
634 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
795d57ce
PP
635 increment_extended_len(event->filter_expression, NULL,
636 &extended_len);
b4e3ceb9
PP
637 }
638
639 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
640 *events = zmalloc(*total_size);
2f77fc4b 641 if (*events == NULL) {
f73fabfd 642 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
643 goto error;
644 }
645
ab4aa612 646 extended_at = ((void *) *events) +
b4e3ceb9
PP
647 nb_event * sizeof(struct lttng_event);
648
2f77fc4b
DG
649 /* Kernel channels */
650 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
651 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
652 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
653 (*events)[i].enabled = event->enabled;
00f992f2
JG
654 (*events)[i].filter =
655 (unsigned char) !!event->filter_expression;
2f77fc4b
DG
656
657 switch (event->event->instrumentation) {
658 case LTTNG_KERNEL_TRACEPOINT:
659 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
660 break;
2f77fc4b 661 case LTTNG_KERNEL_KRETPROBE:
1896972b
DG
662 (*events)[i].type = LTTNG_EVENT_FUNCTION;
663 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
664 sizeof(struct lttng_kernel_kprobe));
665 break;
666 case LTTNG_KERNEL_KPROBE:
2f77fc4b
DG
667 (*events)[i].type = LTTNG_EVENT_PROBE;
668 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
669 sizeof(struct lttng_kernel_kprobe));
670 break;
671 case LTTNG_KERNEL_FUNCTION:
672 (*events)[i].type = LTTNG_EVENT_FUNCTION;
673 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
674 sizeof(struct lttng_kernel_function));
675 break;
676 case LTTNG_KERNEL_NOOP:
677 (*events)[i].type = LTTNG_EVENT_NOOP;
678 break;
679 case LTTNG_KERNEL_SYSCALL:
680 (*events)[i].type = LTTNG_EVENT_SYSCALL;
681 break;
682 case LTTNG_KERNEL_ALL:
683 assert(0);
684 break;
685 }
686 i++;
b4e3ceb9
PP
687
688 /* Append extended info */
795d57ce
PP
689 append_extended_info(event->filter_expression, NULL,
690 &extended_at);
2f77fc4b
DG
691 }
692
db906c12 693end:
2f77fc4b
DG
694 return nb_event;
695
696error:
f73fabfd
DG
697 /* Negate the error code to differentiate the size from an error */
698 return -ret;
2f77fc4b
DG
699}
700
701/*
702 * Add URI so the consumer output object. Set the correct path depending on the
703 * domain adding the default trace directory.
704 */
705static int add_uri_to_consumer(struct consumer_output *consumer,
56a37563
JG
706 struct lttng_uri *uri, enum lttng_domain_type domain,
707 const char *session_name)
2f77fc4b 708{
f73fabfd 709 int ret = LTTNG_OK;
2f77fc4b
DG
710 const char *default_trace_dir;
711
712 assert(uri);
713
714 if (consumer == NULL) {
715 DBG("No consumer detected. Don't add URI. Stopping.");
f73fabfd 716 ret = LTTNG_ERR_NO_CONSUMER;
2f77fc4b
DG
717 goto error;
718 }
719
720 switch (domain) {
721 case LTTNG_DOMAIN_KERNEL:
722 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
723 break;
724 case LTTNG_DOMAIN_UST:
725 default_trace_dir = DEFAULT_UST_TRACE_DIR;
726 break;
727 default:
728 /*
729 * This case is possible is we try to add the URI to the global tracing
730 * session consumer object which in this case there is no subdir.
731 */
732 default_trace_dir = "";
733 }
734
735 switch (uri->dtype) {
736 case LTTNG_DST_IPV4:
737 case LTTNG_DST_IPV6:
738 DBG2("Setting network URI to consumer");
739
df75acac
DG
740 if (consumer->type == CONSUMER_DST_NET) {
741 if ((uri->stype == LTTNG_STREAM_CONTROL &&
785d2d0d
DG
742 consumer->dst.net.control_isset) ||
743 (uri->stype == LTTNG_STREAM_DATA &&
744 consumer->dst.net.data_isset)) {
df75acac
DG
745 ret = LTTNG_ERR_URL_EXIST;
746 goto error;
747 }
748 } else {
749 memset(&consumer->dst.net, 0, sizeof(consumer->dst.net));
785d2d0d
DG
750 }
751
df75acac
DG
752 consumer->type = CONSUMER_DST_NET;
753
2f77fc4b
DG
754 /* Set URI into consumer output object */
755 ret = consumer_set_network_uri(consumer, uri);
756 if (ret < 0) {
a74934ba 757 ret = -ret;
2f77fc4b
DG
758 goto error;
759 } else if (ret == 1) {
760 /*
761 * URI was the same in the consumer so we do not append the subdir
762 * again so to not duplicate output dir.
763 */
f86c9f4e 764 ret = LTTNG_OK;
2f77fc4b
DG
765 goto error;
766 }
767
768 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
769 ret = consumer_set_subdir(consumer, session_name);
770 if (ret < 0) {
f73fabfd 771 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
772 goto error;
773 }
774 }
775
776 if (uri->stype == LTTNG_STREAM_CONTROL) {
777 /* On a new subdir, reappend the default trace dir. */
c30ce0b3
CB
778 strncat(consumer->subdir, default_trace_dir,
779 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
780 DBG3("Append domain trace name to subdir %s", consumer->subdir);
781 }
782
783 break;
784 case LTTNG_DST_PATH:
785 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
786 memset(consumer->dst.trace_path, 0,
787 sizeof(consumer->dst.trace_path));
9ac05d92
MD
788 /* Explicit length checks for strcpy and strcat. */
789 if (strlen(uri->dst.path) + strlen(default_trace_dir)
790 >= sizeof(consumer->dst.trace_path)) {
791 ret = LTTNG_ERR_FATAL;
792 goto error;
793 }
794 strcpy(consumer->dst.trace_path, uri->dst.path);
2f77fc4b 795 /* Append default trace dir */
9ac05d92 796 strcat(consumer->dst.trace_path, default_trace_dir);
2f77fc4b
DG
797 /* Flag consumer as local. */
798 consumer->type = CONSUMER_DST_LOCAL;
799 break;
800 }
801
a74934ba
DG
802 ret = LTTNG_OK;
803
2f77fc4b
DG
804error:
805 return ret;
806}
807
808/*
809 * Init tracing by creating trace directory and sending fds kernel consumer.
810 */
811static int init_kernel_tracing(struct ltt_kernel_session *session)
812{
813 int ret = 0;
814 struct lttng_ht_iter iter;
815 struct consumer_socket *socket;
816
817 assert(session);
818
e7fe706f
DG
819 rcu_read_lock();
820
2f77fc4b
DG
821 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
822 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
823 socket, node.node) {
2f77fc4b 824 pthread_mutex_lock(socket->lock);
f50f23d9 825 ret = kernel_consumer_send_session(socket, session);
2f77fc4b
DG
826 pthread_mutex_unlock(socket->lock);
827 if (ret < 0) {
f73fabfd 828 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
829 goto error;
830 }
831 }
832 }
833
834error:
e7fe706f 835 rcu_read_unlock();
2f77fc4b
DG
836 return ret;
837}
838
839/*
840 * Create a socket to the relayd using the URI.
841 *
842 * On success, the relayd_sock pointer is set to the created socket.
843 * Else, it's stays untouched and a lttcomm error code is returned.
844 */
6151a90f 845static int create_connect_relayd(struct lttng_uri *uri,
b31610f2
JD
846 struct lttcomm_relayd_sock **relayd_sock,
847 struct consumer_output *consumer)
2f77fc4b
DG
848{
849 int ret;
6151a90f 850 struct lttcomm_relayd_sock *rsock;
2f77fc4b 851
6151a90f
JD
852 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
853 RELAYD_VERSION_COMM_MINOR);
854 if (!rsock) {
f73fabfd 855 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
856 goto error;
857 }
858
ffe60014
DG
859 /*
860 * Connect to relayd so we can proceed with a session creation. This call
861 * can possibly block for an arbitrary amount of time to set the health
862 * state to be in poll execution.
863 */
864 health_poll_entry();
6151a90f 865 ret = relayd_connect(rsock);
ffe60014 866 health_poll_exit();
2f77fc4b
DG
867 if (ret < 0) {
868 ERR("Unable to reach lttng-relayd");
f73fabfd 869 ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
870 goto free_sock;
871 }
872
873 /* Create socket for control stream. */
874 if (uri->stype == LTTNG_STREAM_CONTROL) {
875 DBG3("Creating relayd stream socket from URI");
876
877 /* Check relayd version */
6151a90f 878 ret = relayd_version_check(rsock);
2f77fc4b 879 if (ret < 0) {
f73fabfd 880 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
2f77fc4b
DG
881 goto close_sock;
882 }
b31610f2
JD
883 consumer->relay_major_version = rsock->major;
884 consumer->relay_minor_version = rsock->minor;
2f77fc4b
DG
885 } else if (uri->stype == LTTNG_STREAM_DATA) {
886 DBG3("Creating relayd data socket from URI");
887 } else {
888 /* Command is not valid */
889 ERR("Relayd invalid stream type: %d", uri->stype);
f73fabfd 890 ret = LTTNG_ERR_INVALID;
2f77fc4b
DG
891 goto close_sock;
892 }
893
6151a90f 894 *relayd_sock = rsock;
2f77fc4b 895
f73fabfd 896 return LTTNG_OK;
2f77fc4b
DG
897
898close_sock:
6151a90f
JD
899 /* The returned value is not useful since we are on an error path. */
900 (void) relayd_close(rsock);
2f77fc4b 901free_sock:
6151a90f 902 free(rsock);
2f77fc4b
DG
903error:
904 return ret;
905}
906
907/*
908 * Connect to the relayd using URI and send the socket to the right consumer.
909 */
56a37563
JG
910static int send_consumer_relayd_socket(enum lttng_domain_type domain,
911 unsigned int session_id, struct lttng_uri *relayd_uri,
912 struct consumer_output *consumer,
d3e2ba59
JD
913 struct consumer_socket *consumer_sock,
914 char *session_name, char *hostname, int session_live_timer)
2f77fc4b
DG
915{
916 int ret;
6151a90f 917 struct lttcomm_relayd_sock *rsock = NULL;
2f77fc4b 918
ffe60014 919 /* Connect to relayd and make version check if uri is the control. */
b31610f2 920 ret = create_connect_relayd(relayd_uri, &rsock, consumer);
ffe60014 921 if (ret != LTTNG_OK) {
6151a90f 922 goto error;
ffe60014 923 }
6151a90f 924 assert(rsock);
ffe60014 925
2f77fc4b 926 /* Set the network sequence index if not set. */
d88aee68
DG
927 if (consumer->net_seq_index == (uint64_t) -1ULL) {
928 pthread_mutex_lock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
929 /*
930 * Increment net_seq_idx because we are about to transfer the
931 * new relayd socket to the consumer.
d88aee68 932 * Assign unique key so the consumer can match streams.
2f77fc4b 933 */
d88aee68
DG
934 consumer->net_seq_index = ++relayd_net_seq_idx;
935 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
936 }
937
2f77fc4b 938 /* Send relayd socket to consumer. */
6151a90f 939 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
d3e2ba59
JD
940 relayd_uri->stype, session_id,
941 session_name, hostname, session_live_timer);
2f77fc4b 942 if (ret < 0) {
f73fabfd 943 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2f77fc4b
DG
944 goto close_sock;
945 }
946
c890b720
DG
947 /* Flag that the corresponding socket was sent. */
948 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
ffe60014 949 consumer_sock->control_sock_sent = 1;
c890b720 950 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
ffe60014 951 consumer_sock->data_sock_sent = 1;
c890b720
DG
952 }
953
f73fabfd 954 ret = LTTNG_OK;
2f77fc4b
DG
955
956 /*
957 * Close socket which was dup on the consumer side. The session daemon does
958 * NOT keep track of the relayd socket(s) once transfer to the consumer.
959 */
960
961close_sock:
6151a90f
JD
962 (void) relayd_close(rsock);
963 free(rsock);
2f77fc4b 964
6151a90f 965error:
ffe60014
DG
966 if (ret != LTTNG_OK) {
967 /*
d9078d0c
DG
968 * The consumer output for this session should not be used anymore
969 * since the relayd connection failed thus making any tracing or/and
970 * streaming not usable.
ffe60014 971 */
d9078d0c 972 consumer->enabled = 0;
ffe60014 973 }
2f77fc4b
DG
974 return ret;
975}
976
977/*
978 * Send both relayd sockets to a specific consumer and domain. This is a
979 * helper function to facilitate sending the information to the consumer for a
980 * session.
981 */
56a37563
JG
982static int send_consumer_relayd_sockets(enum lttng_domain_type domain,
983 unsigned int session_id, struct consumer_output *consumer,
984 struct consumer_socket *sock, char *session_name,
985 char *hostname, int session_live_timer)
2f77fc4b 986{
dda67f6c 987 int ret = LTTNG_OK;
2f77fc4b 988
2f77fc4b 989 assert(consumer);
6dc3064a 990 assert(sock);
2f77fc4b 991
2f77fc4b 992 /* Sending control relayd socket. */
ffe60014 993 if (!sock->control_sock_sent) {
6dc3064a 994 ret = send_consumer_relayd_socket(domain, session_id,
d3e2ba59
JD
995 &consumer->dst.net.control, consumer, sock,
996 session_name, hostname, session_live_timer);
c890b720
DG
997 if (ret != LTTNG_OK) {
998 goto error;
999 }
2f77fc4b
DG
1000 }
1001
1002 /* Sending data relayd socket. */
ffe60014 1003 if (!sock->data_sock_sent) {
6dc3064a 1004 ret = send_consumer_relayd_socket(domain, session_id,
d3e2ba59
JD
1005 &consumer->dst.net.data, consumer, sock,
1006 session_name, hostname, session_live_timer);
c890b720
DG
1007 if (ret != LTTNG_OK) {
1008 goto error;
1009 }
2f77fc4b
DG
1010 }
1011
2f77fc4b
DG
1012error:
1013 return ret;
1014}
1015
1016/*
1017 * Setup relayd connections for a tracing session. First creates the socket to
1018 * the relayd and send them to the right domain consumer. Consumer type MUST be
1019 * network.
1020 */
ffe60014 1021int cmd_setup_relayd(struct ltt_session *session)
2f77fc4b 1022{
f73fabfd 1023 int ret = LTTNG_OK;
2f77fc4b
DG
1024 struct ltt_ust_session *usess;
1025 struct ltt_kernel_session *ksess;
1026 struct consumer_socket *socket;
1027 struct lttng_ht_iter iter;
1028
1029 assert(session);
1030
1031 usess = session->ust_session;
1032 ksess = session->kernel_session;
1033
785d2d0d 1034 DBG("Setting relayd for session %s", session->name);
2f77fc4b 1035
e7fe706f
DG
1036 rcu_read_lock();
1037
2f77fc4b
DG
1038 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1039 && usess->consumer->enabled) {
1040 /* For each consumer socket, send relayd sockets */
1041 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1042 socket, node.node) {
2f77fc4b 1043 pthread_mutex_lock(socket->lock);
6dc3064a 1044 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
d3e2ba59
JD
1045 usess->consumer, socket,
1046 session->name, session->hostname,
1047 session->live_timer);
2f77fc4b 1048 pthread_mutex_unlock(socket->lock);
f73fabfd 1049 if (ret != LTTNG_OK) {
2f77fc4b
DG
1050 goto error;
1051 }
6dc3064a
DG
1052 /* Session is now ready for network streaming. */
1053 session->net_handle = 1;
2f77fc4b 1054 }
b31610f2
JD
1055 session->consumer->relay_major_version =
1056 usess->consumer->relay_major_version;
1057 session->consumer->relay_minor_version =
1058 usess->consumer->relay_minor_version;
2f77fc4b
DG
1059 }
1060
1061 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1062 && ksess->consumer->enabled) {
1063 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1064 socket, node.node) {
2f77fc4b 1065 pthread_mutex_lock(socket->lock);
6dc3064a 1066 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
d3e2ba59
JD
1067 ksess->consumer, socket,
1068 session->name, session->hostname,
1069 session->live_timer);
2f77fc4b 1070 pthread_mutex_unlock(socket->lock);
f73fabfd 1071 if (ret != LTTNG_OK) {
2f77fc4b
DG
1072 goto error;
1073 }
6dc3064a
DG
1074 /* Session is now ready for network streaming. */
1075 session->net_handle = 1;
2f77fc4b 1076 }
b31610f2
JD
1077 session->consumer->relay_major_version =
1078 ksess->consumer->relay_major_version;
1079 session->consumer->relay_minor_version =
1080 ksess->consumer->relay_minor_version;
2f77fc4b
DG
1081 }
1082
1083error:
e7fe706f 1084 rcu_read_unlock();
2f77fc4b
DG
1085 return ret;
1086}
1087
9b6c7ec5
DG
1088/*
1089 * Start a kernel session by opening all necessary streams.
1090 */
1091static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
1092{
1093 int ret;
1094 struct ltt_kernel_channel *kchan;
1095
1096 /* Open kernel metadata */
07b86b52 1097 if (ksess->metadata == NULL && ksess->output_traces) {
9b6c7ec5
DG
1098 ret = kernel_open_metadata(ksess);
1099 if (ret < 0) {
1100 ret = LTTNG_ERR_KERN_META_FAIL;
1101 goto error;
1102 }
1103 }
1104
1105 /* Open kernel metadata stream */
07b86b52 1106 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
9b6c7ec5
DG
1107 ret = kernel_open_metadata_stream(ksess);
1108 if (ret < 0) {
1109 ERR("Kernel create metadata stream failed");
1110 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1111 goto error;
1112 }
1113 }
1114
1115 /* For each channel */
1116 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1117 if (kchan->stream_count == 0) {
1118 ret = kernel_open_channel_stream(kchan);
1119 if (ret < 0) {
1120 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1121 goto error;
1122 }
1123 /* Update the stream global counter */
1124 ksess->stream_count_global += ret;
1125 }
1126 }
1127
1128 /* Setup kernel consumer socket and send fds to it */
1129 ret = init_kernel_tracing(ksess);
e43c41c5 1130 if (ret != 0) {
9b6c7ec5
DG
1131 ret = LTTNG_ERR_KERN_START_FAIL;
1132 goto error;
1133 }
1134
1135 /* This start the kernel tracing */
1136 ret = kernel_start_session(ksess);
1137 if (ret < 0) {
1138 ret = LTTNG_ERR_KERN_START_FAIL;
1139 goto error;
1140 }
1141
1142 /* Quiescent wait after starting trace */
784303b0 1143 kernel_wait_quiescent(kernel_tracer_fd);
9b6c7ec5 1144
14fb1ebe 1145 ksess->active = 1;
9b6c7ec5
DG
1146
1147 ret = LTTNG_OK;
1148
1149error:
1150 return ret;
1151}
1152
2f77fc4b
DG
1153/*
1154 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1155 */
56a37563
JG
1156int cmd_disable_channel(struct ltt_session *session,
1157 enum lttng_domain_type domain, char *channel_name)
2f77fc4b
DG
1158{
1159 int ret;
1160 struct ltt_ust_session *usess;
1161
1162 usess = session->ust_session;
1163
2223c96f
DG
1164 rcu_read_lock();
1165
2f77fc4b
DG
1166 switch (domain) {
1167 case LTTNG_DOMAIN_KERNEL:
1168 {
1169 ret = channel_kernel_disable(session->kernel_session,
1170 channel_name);
f73fabfd 1171 if (ret != LTTNG_OK) {
2f77fc4b
DG
1172 goto error;
1173 }
1174
1175 kernel_wait_quiescent(kernel_tracer_fd);
1176 break;
1177 }
1178 case LTTNG_DOMAIN_UST:
1179 {
1180 struct ltt_ust_channel *uchan;
1181 struct lttng_ht *chan_ht;
1182
1183 chan_ht = usess->domain_global.channels;
1184
1185 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1186 if (uchan == NULL) {
f73fabfd 1187 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1188 goto error;
1189 }
1190
7972aab2 1191 ret = channel_ust_disable(usess, uchan);
f73fabfd 1192 if (ret != LTTNG_OK) {
2f77fc4b
DG
1193 goto error;
1194 }
1195 break;
1196 }
2f77fc4b 1197 default:
f73fabfd 1198 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1199 goto error;
1200 }
1201
f73fabfd 1202 ret = LTTNG_OK;
2f77fc4b
DG
1203
1204error:
2223c96f 1205 rcu_read_unlock();
2f77fc4b
DG
1206 return ret;
1207}
1208
ccf10263
MD
1209/*
1210 * Command LTTNG_TRACK_PID processed by the client thread.
a9ad0c8f
MD
1211 *
1212 * Called with session lock held.
ccf10263 1213 */
56a37563
JG
1214int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
1215 int pid)
ccf10263
MD
1216{
1217 int ret;
1218
1219 rcu_read_lock();
1220
1221 switch (domain) {
1222 case LTTNG_DOMAIN_KERNEL:
1223 {
1224 struct ltt_kernel_session *ksess;
1225
1226 ksess = session->kernel_session;
1227
1228 ret = kernel_track_pid(ksess, pid);
1229 if (ret != LTTNG_OK) {
1230 goto error;
1231 }
1232
1233 kernel_wait_quiescent(kernel_tracer_fd);
1234 break;
1235 }
ccf10263 1236 case LTTNG_DOMAIN_UST:
a9ad0c8f
MD
1237 {
1238 struct ltt_ust_session *usess;
1239
1240 usess = session->ust_session;
1241
1242 ret = trace_ust_track_pid(usess, pid);
1243 if (ret != LTTNG_OK) {
1244 goto error;
1245 }
1246 break;
1247 }
ccf10263
MD
1248 default:
1249 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1250 goto error;
1251 }
1252
1253 ret = LTTNG_OK;
1254
1255error:
1256 rcu_read_unlock();
1257 return ret;
1258}
1259
1260/*
1261 * Command LTTNG_UNTRACK_PID processed by the client thread.
a9ad0c8f
MD
1262 *
1263 * Called with session lock held.
ccf10263 1264 */
56a37563
JG
1265int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
1266 int pid)
ccf10263
MD
1267{
1268 int ret;
1269
1270 rcu_read_lock();
1271
1272 switch (domain) {
1273 case LTTNG_DOMAIN_KERNEL:
1274 {
1275 struct ltt_kernel_session *ksess;
1276
1277 ksess = session->kernel_session;
1278
1279 ret = kernel_untrack_pid(ksess, pid);
1280 if (ret != LTTNG_OK) {
1281 goto error;
1282 }
1283
1284 kernel_wait_quiescent(kernel_tracer_fd);
1285 break;
1286 }
ccf10263 1287 case LTTNG_DOMAIN_UST:
a9ad0c8f
MD
1288 {
1289 struct ltt_ust_session *usess;
1290
1291 usess = session->ust_session;
1292
1293 ret = trace_ust_untrack_pid(usess, pid);
1294 if (ret != LTTNG_OK) {
1295 goto error;
1296 }
1297 break;
1298 }
ccf10263
MD
1299 default:
1300 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1301 goto error;
1302 }
1303
1304 ret = LTTNG_OK;
1305
1306error:
1307 rcu_read_unlock();
1308 return ret;
1309}
1310
2f77fc4b
DG
1311/*
1312 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1313 *
1314 * The wpipe arguments is used as a notifier for the kernel thread.
1315 */
1316int cmd_enable_channel(struct ltt_session *session,
7972aab2 1317 struct lttng_domain *domain, struct lttng_channel *attr, int wpipe)
2f77fc4b
DG
1318{
1319 int ret;
1320 struct ltt_ust_session *usess = session->ust_session;
1321 struct lttng_ht *chan_ht;
1f345e94 1322 size_t len;
2f77fc4b
DG
1323
1324 assert(session);
1325 assert(attr);
7972aab2 1326 assert(domain);
2f77fc4b 1327
f5436bfc 1328 len = lttng_strnlen(attr->name, sizeof(attr->name));
1f345e94
PP
1329
1330 /* Validate channel name */
1331 if (attr->name[0] == '.' ||
1332 memchr(attr->name, '/', len) != NULL) {
1333 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1334 goto end;
1335 }
1336
2f77fc4b
DG
1337 DBG("Enabling channel %s for session %s", attr->name, session->name);
1338
03b4fdcf
DG
1339 rcu_read_lock();
1340
ecc48a90
JD
1341 /*
1342 * Don't try to enable a channel if the session has been started at
1343 * some point in time before. The tracer does not allow it.
1344 */
8382cf6f 1345 if (session->has_been_started) {
ecc48a90
JD
1346 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1347 goto error;
1348 }
1349
1350 /*
1351 * If the session is a live session, remove the switch timer, the
1352 * live timer does the same thing but sends also synchronisation
1353 * beacons for inactive streams.
1354 */
1355 if (session->live_timer > 0) {
1356 attr->attr.live_timer_interval = session->live_timer;
1357 attr->attr.switch_timer_interval = 0;
1358 }
1359
7972aab2 1360 switch (domain->type) {
2f77fc4b
DG
1361 case LTTNG_DOMAIN_KERNEL:
1362 {
1363 struct ltt_kernel_channel *kchan;
1364
2f77fc4b
DG
1365 kchan = trace_kernel_get_channel_by_name(attr->name,
1366 session->kernel_session);
1367 if (kchan == NULL) {
1368 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
85076754
MD
1369 if (attr->name[0] != '\0') {
1370 session->kernel_session->has_non_default_channel = 1;
1371 }
2f77fc4b
DG
1372 } else {
1373 ret = channel_kernel_enable(session->kernel_session, kchan);
1374 }
1375
f73fabfd 1376 if (ret != LTTNG_OK) {
2f77fc4b
DG
1377 goto error;
1378 }
1379
784303b0 1380 kernel_wait_quiescent(kernel_tracer_fd);
2f77fc4b
DG
1381 break;
1382 }
1383 case LTTNG_DOMAIN_UST:
9232818f
JG
1384 case LTTNG_DOMAIN_JUL:
1385 case LTTNG_DOMAIN_LOG4J:
1386 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
1387 {
1388 struct ltt_ust_channel *uchan;
1389
9232818f
JG
1390 /*
1391 * FIXME
1392 *
1393 * Current agent implementation limitations force us to allow
1394 * only one channel at once in "agent" subdomains. Each
1395 * subdomain has a default channel name which must be strictly
1396 * adhered to.
1397 */
1398 if (domain->type == LTTNG_DOMAIN_JUL) {
1399 if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME,
1400 LTTNG_SYMBOL_NAME_LEN)) {
1401 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1402 goto error;
1403 }
1404 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1405 if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME,
1406 LTTNG_SYMBOL_NAME_LEN)) {
1407 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1408 goto error;
1409 }
1410 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1411 if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME,
1412 LTTNG_SYMBOL_NAME_LEN)) {
1413 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1414 goto error;
1415 }
1416 }
1417
2f77fc4b
DG
1418 chan_ht = usess->domain_global.channels;
1419
1420 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1421 if (uchan == NULL) {
7972aab2 1422 ret = channel_ust_create(usess, attr, domain->buf_type);
85076754
MD
1423 if (attr->name[0] != '\0') {
1424 usess->has_non_default_channel = 1;
1425 }
2f77fc4b 1426 } else {
7972aab2 1427 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1428 }
1429 break;
1430 }
2f77fc4b 1431 default:
f73fabfd 1432 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1433 goto error;
1434 }
1435
1436error:
2223c96f 1437 rcu_read_unlock();
1f345e94 1438end:
2f77fc4b
DG
1439 return ret;
1440}
1441
2f77fc4b
DG
1442/*
1443 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1444 */
56a37563
JG
1445int cmd_disable_event(struct ltt_session *session,
1446 enum lttng_domain_type domain, char *channel_name,
6e911cad 1447 struct lttng_event *event)
2f77fc4b
DG
1448{
1449 int ret;
6e911cad
MD
1450 char *event_name;
1451
18a720cd
MD
1452 DBG("Disable event command for event \'%s\'", event->name);
1453
6e911cad
MD
1454 event_name = event->name;
1455
9b7431cf
JG
1456 /* Error out on unhandled search criteria */
1457 if (event->loglevel_type || event->loglevel != -1 || event->enabled
6e911cad 1458 || event->pid || event->filter || event->exclusion) {
7076b56e
JG
1459 ret = LTTNG_ERR_UNK;
1460 goto error;
6e911cad 1461 }
2f77fc4b 1462
2223c96f
DG
1463 rcu_read_lock();
1464
2f77fc4b
DG
1465 switch (domain) {
1466 case LTTNG_DOMAIN_KERNEL:
1467 {
1468 struct ltt_kernel_channel *kchan;
1469 struct ltt_kernel_session *ksess;
1470
1471 ksess = session->kernel_session;
1472
85076754
MD
1473 /*
1474 * If a non-default channel has been created in the
1475 * session, explicitely require that -c chan_name needs
1476 * to be provided.
1477 */
1478 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1479 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1480 goto error_unlock;
85076754
MD
1481 }
1482
2f77fc4b
DG
1483 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1484 if (kchan == NULL) {
f73fabfd 1485 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
7076b56e 1486 goto error_unlock;
2f77fc4b
DG
1487 }
1488
6e911cad
MD
1489 switch (event->type) {
1490 case LTTNG_EVENT_ALL:
9550ee81 1491 case LTTNG_EVENT_TRACEPOINT:
d0ae4ea8 1492 case LTTNG_EVENT_SYSCALL:
9550ee81
JR
1493 case LTTNG_EVENT_PROBE:
1494 case LTTNG_EVENT_FUNCTION:
1495 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1496 if (event_name[0] == '\0') {
1497 ret = event_kernel_disable_event(kchan,
1498 NULL, event->type);
29c62722 1499 } else {
d0ae4ea8 1500 ret = event_kernel_disable_event(kchan,
9550ee81 1501 event_name, event->type);
29c62722 1502 }
6e911cad 1503 if (ret != LTTNG_OK) {
7076b56e 1504 goto error_unlock;
6e911cad
MD
1505 }
1506 break;
6e911cad
MD
1507 default:
1508 ret = LTTNG_ERR_UNK;
7076b56e 1509 goto error_unlock;
2f77fc4b
DG
1510 }
1511
1512 kernel_wait_quiescent(kernel_tracer_fd);
1513 break;
1514 }
1515 case LTTNG_DOMAIN_UST:
1516 {
1517 struct ltt_ust_channel *uchan;
1518 struct ltt_ust_session *usess;
1519
1520 usess = session->ust_session;
1521
7076b56e
JG
1522 if (validate_ust_event_name(event_name)) {
1523 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1524 goto error_unlock;
1525 }
1526
85076754
MD
1527 /*
1528 * If a non-default channel has been created in the
9550ee81 1529 * session, explicitly require that -c chan_name needs
85076754
MD
1530 * to be provided.
1531 */
1532 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1533 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1534 goto error_unlock;
85076754
MD
1535 }
1536
2f77fc4b
DG
1537 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1538 channel_name);
1539 if (uchan == NULL) {
f73fabfd 1540 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
7076b56e 1541 goto error_unlock;
2f77fc4b
DG
1542 }
1543
6e911cad
MD
1544 switch (event->type) {
1545 case LTTNG_EVENT_ALL:
b3639870
JR
1546 /*
1547 * An empty event name means that everything
1548 * should be disabled.
1549 */
1550 if (event->name[0] == '\0') {
1551 ret = event_ust_disable_all_tracepoints(usess, uchan);
77d536b2
JR
1552 } else {
1553 ret = event_ust_disable_tracepoint(usess, uchan,
1554 event_name);
1555 }
6e911cad 1556 if (ret != LTTNG_OK) {
7076b56e 1557 goto error_unlock;
6e911cad
MD
1558 }
1559 break;
1560 default:
1561 ret = LTTNG_ERR_UNK;
7076b56e 1562 goto error_unlock;
2f77fc4b
DG
1563 }
1564
1565 DBG3("Disable UST event %s in channel %s completed", event_name,
1566 channel_name);
1567 break;
1568 }
5cdb6027 1569 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1570 case LTTNG_DOMAIN_JUL:
0e115563 1571 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1572 {
fefd409b 1573 struct agent *agt;
f20baf8e
DG
1574 struct ltt_ust_session *usess = session->ust_session;
1575
1576 assert(usess);
1577
6e911cad
MD
1578 switch (event->type) {
1579 case LTTNG_EVENT_ALL:
1580 break;
1581 default:
1582 ret = LTTNG_ERR_UNK;
7076b56e 1583 goto error_unlock;
6e911cad
MD
1584 }
1585
5cdb6027 1586 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1587 if (!agt) {
1588 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
7076b56e 1589 goto error_unlock;
fefd409b 1590 }
b3639870
JR
1591 /*
1592 * An empty event name means that everything
1593 * should be disabled.
1594 */
1595 if (event->name[0] == '\0') {
18a720cd
MD
1596 ret = event_agent_disable_all(usess, agt);
1597 } else {
1598 ret = event_agent_disable(usess, agt, event_name);
1599 }
f20baf8e 1600 if (ret != LTTNG_OK) {
7076b56e 1601 goto error_unlock;
f20baf8e
DG
1602 }
1603
1604 break;
1605 }
2f77fc4b 1606 default:
f73fabfd 1607 ret = LTTNG_ERR_UND;
7076b56e 1608 goto error_unlock;
2f77fc4b
DG
1609 }
1610
f73fabfd 1611 ret = LTTNG_OK;
2f77fc4b 1612
7076b56e 1613error_unlock:
2223c96f 1614 rcu_read_unlock();
7076b56e 1615error:
2f77fc4b
DG
1616 return ret;
1617}
1618
2f77fc4b
DG
1619/*
1620 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1621 */
56a37563 1622int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
601d5acf 1623 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1624{
d5979e4a 1625 int ret, chan_kern_created = 0, chan_ust_created = 0;
bdf64013
JG
1626 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1627
1628 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1629 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1630 app_ctx_name = ctx->u.app_ctx.ctx_name;
1631 }
2f77fc4b
DG
1632
1633 switch (domain) {
1634 case LTTNG_DOMAIN_KERNEL:
979e618e
DG
1635 assert(session->kernel_session);
1636
1637 if (session->kernel_session->channel_count == 0) {
1638 /* Create default channel */
1639 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1640 if (ret != LTTNG_OK) {
1641 goto error;
1642 }
d5979e4a 1643 chan_kern_created = 1;
979e618e 1644 }
2f77fc4b 1645 /* Add kernel context to kernel tracer */
601d5acf 1646 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 1647 if (ret != LTTNG_OK) {
2f77fc4b
DG
1648 goto error;
1649 }
1650 break;
bdf64013
JG
1651 case LTTNG_DOMAIN_JUL:
1652 case LTTNG_DOMAIN_LOG4J:
1653 {
1654 /*
1655 * Validate channel name.
1656 * If no channel name is given and the domain is JUL or LOG4J,
1657 * set it to the appropriate domain-specific channel name. If
1658 * a name is provided but does not match the expexted channel
1659 * name, return an error.
1660 */
1661 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1662 strcmp(channel_name,
1663 DEFAULT_JUL_CHANNEL_NAME)) {
1664 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1665 goto error;
1666 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1667 strcmp(channel_name,
1668 DEFAULT_LOG4J_CHANNEL_NAME)) {
1669 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1670 goto error;
1671 }
a93b3916 1672 /* break is _not_ missing here. */
bdf64013 1673 }
2f77fc4b
DG
1674 case LTTNG_DOMAIN_UST:
1675 {
1676 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
1677 unsigned int chan_count;
1678
2f77fc4b
DG
1679 assert(usess);
1680
85076754 1681 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
1682 if (chan_count == 0) {
1683 struct lttng_channel *attr;
1684 /* Create default channel */
0a9c6494 1685 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
1686 if (attr == NULL) {
1687 ret = LTTNG_ERR_FATAL;
1688 goto error;
1689 }
1690
7972aab2 1691 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
1692 if (ret != LTTNG_OK) {
1693 free(attr);
1694 goto error;
1695 }
cf0bcb51 1696 channel_attr_destroy(attr);
d5979e4a 1697 chan_ust_created = 1;
979e618e
DG
1698 }
1699
601d5acf 1700 ret = context_ust_add(usess, domain, ctx, channel_name);
bdf64013
JG
1701 free(app_ctx_provider_name);
1702 free(app_ctx_name);
1703 app_ctx_name = NULL;
1704 app_ctx_provider_name = NULL;
f73fabfd 1705 if (ret != LTTNG_OK) {
2f77fc4b
DG
1706 goto error;
1707 }
1708 break;
1709 }
2f77fc4b 1710 default:
f73fabfd 1711 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1712 goto error;
1713 }
1714
bdf64013
JG
1715 ret = LTTNG_OK;
1716 goto end;
2f77fc4b
DG
1717
1718error:
d5979e4a
DG
1719 if (chan_kern_created) {
1720 struct ltt_kernel_channel *kchan =
1721 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1722 session->kernel_session);
1723 /* Created previously, this should NOT fail. */
1724 assert(kchan);
1725 kernel_destroy_channel(kchan);
1726 }
1727
1728 if (chan_ust_created) {
1729 struct ltt_ust_channel *uchan =
1730 trace_ust_find_channel_by_name(
1731 session->ust_session->domain_global.channels,
1732 DEFAULT_CHANNEL_NAME);
1733 /* Created previously, this should NOT fail. */
1734 assert(uchan);
1735 /* Remove from the channel list of the session. */
1736 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1737 uchan);
1738 trace_ust_destroy_channel(uchan);
1739 }
bdf64013
JG
1740end:
1741 free(app_ctx_provider_name);
1742 free(app_ctx_name);
2f77fc4b
DG
1743 return ret;
1744}
1745
dac8e046
JG
1746static inline bool name_starts_with(const char *name, const char *prefix)
1747{
1748 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
1749
1750 return !strncmp(name, prefix, max_cmp_len);
1751}
1752
1753/* Perform userspace-specific event name validation */
1754static int validate_ust_event_name(const char *name)
1755{
1756 int ret = 0;
1757
1758 if (!name) {
1759 ret = -1;
1760 goto end;
1761 }
1762
1763 /*
1764 * Check name against all internal UST event component namespaces used
1765 * by the agents.
1766 */
1767 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
1768 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
1769 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
1770 ret = -1;
1771 }
1772
1773end:
1774 return ret;
1775}
88f06f15 1776
2f77fc4b 1777/*
88f06f15
JG
1778 * Internal version of cmd_enable_event() with a supplemental
1779 * "internal_event" flag which is used to enable internal events which should
1780 * be hidden from clients. Such events are used in the agent implementation to
1781 * enable the events through which all "agent" events are funeled.
2f77fc4b 1782 */
88f06f15
JG
1783static int _cmd_enable_event(struct ltt_session *session,
1784 struct lttng_domain *domain,
025faf73 1785 char *channel_name, struct lttng_event *event,
6b453b5e 1786 char *filter_expression,
db8f1377
JI
1787 struct lttng_filter_bytecode *filter,
1788 struct lttng_event_exclusion *exclusion,
88f06f15 1789 int wpipe, bool internal_event)
2f77fc4b 1790{
9f449915 1791 int ret = 0, channel_created = 0;
cfedea03 1792 struct lttng_channel *attr = NULL;
2f77fc4b
DG
1793
1794 assert(session);
1795 assert(event);
1796 assert(channel_name);
1797
2a385866
JG
1798 /* If we have a filter, we must have its filter expression */
1799 assert(!(!!filter_expression ^ !!filter));
1800
9f449915
PP
1801 /* Normalize event name as a globbing pattern */
1802 strutils_normalize_star_glob_pattern(event->name);
18a720cd 1803
9f449915
PP
1804 /* Normalize exclusion names as globbing patterns */
1805 if (exclusion) {
1806 size_t i;
f5ac4bd7 1807
9f449915
PP
1808 for (i = 0; i < exclusion->count; i++) {
1809 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
1810
1811 strutils_normalize_star_glob_pattern(name);
1812 }
930a2e99
JG
1813 }
1814
9f449915
PP
1815 DBG("Enable event command for event \'%s\'", event->name);
1816
1817 rcu_read_lock();
1818
7972aab2 1819 switch (domain->type) {
2f77fc4b
DG
1820 case LTTNG_DOMAIN_KERNEL:
1821 {
1822 struct ltt_kernel_channel *kchan;
1823
85076754
MD
1824 /*
1825 * If a non-default channel has been created in the
1826 * session, explicitely require that -c chan_name needs
1827 * to be provided.
1828 */
1829 if (session->kernel_session->has_non_default_channel
1830 && channel_name[0] == '\0') {
1831 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1832 goto error;
1833 }
1834
2f77fc4b
DG
1835 kchan = trace_kernel_get_channel_by_name(channel_name,
1836 session->kernel_session);
1837 if (kchan == NULL) {
0a9c6494
DG
1838 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1839 LTTNG_BUFFER_GLOBAL);
2f77fc4b 1840 if (attr == NULL) {
f73fabfd 1841 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1842 goto error;
1843 }
04c17253
MD
1844 if (lttng_strncpy(attr->name, channel_name,
1845 sizeof(attr->name))) {
1846 ret = LTTNG_ERR_INVALID;
04c17253
MD
1847 goto error;
1848 }
2f77fc4b
DG
1849
1850 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1851 if (ret != LTTNG_OK) {
2f77fc4b
DG
1852 goto error;
1853 }
e5f5db7f 1854 channel_created = 1;
2f77fc4b
DG
1855 }
1856
1857 /* Get the newly created kernel channel pointer */
1858 kchan = trace_kernel_get_channel_by_name(channel_name,
1859 session->kernel_session);
1860 if (kchan == NULL) {
1861 /* This sould not happen... */
f73fabfd 1862 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1863 goto error;
1864 }
1865
6e911cad
MD
1866 switch (event->type) {
1867 case LTTNG_EVENT_ALL:
29c62722 1868 {
00a62084
MD
1869 char *filter_expression_a = NULL;
1870 struct lttng_filter_bytecode *filter_a = NULL;
1871
1872 /*
1873 * We need to duplicate filter_expression and filter,
1874 * because ownership is passed to first enable
1875 * event.
1876 */
1877 if (filter_expression) {
1878 filter_expression_a = strdup(filter_expression);
1879 if (!filter_expression_a) {
1880 ret = LTTNG_ERR_FATAL;
1881 goto error;
1882 }
1883 }
1884 if (filter) {
1885 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
1886 if (!filter_a) {
1887 free(filter_expression_a);
1888 ret = LTTNG_ERR_FATAL;
1889 goto error;
1890 }
1891 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
1892 }
29c62722 1893 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
00a62084
MD
1894 ret = event_kernel_enable_event(kchan, event,
1895 filter_expression, filter);
a969e101
MD
1896 /* We have passed ownership */
1897 filter_expression = NULL;
1898 filter = NULL;
29c62722
MD
1899 if (ret != LTTNG_OK) {
1900 if (channel_created) {
1901 /* Let's not leak a useless channel. */
1902 kernel_destroy_channel(kchan);
1903 }
00a62084
MD
1904 free(filter_expression_a);
1905 free(filter_a);
29c62722
MD
1906 goto error;
1907 }
1908 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
00a62084
MD
1909 ret = event_kernel_enable_event(kchan, event,
1910 filter_expression_a, filter_a);
60d21fa2
AB
1911 /* We have passed ownership */
1912 filter_expression_a = NULL;
1913 filter_a = NULL;
29c62722
MD
1914 if (ret != LTTNG_OK) {
1915 goto error;
1916 }
1917 break;
1918 }
6e6ef3d7
DG
1919 case LTTNG_EVENT_PROBE:
1920 case LTTNG_EVENT_FUNCTION:
1921 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 1922 case LTTNG_EVENT_TRACEPOINT:
00a62084
MD
1923 ret = event_kernel_enable_event(kchan, event,
1924 filter_expression, filter);
a969e101
MD
1925 /* We have passed ownership */
1926 filter_expression = NULL;
1927 filter = NULL;
6e911cad
MD
1928 if (ret != LTTNG_OK) {
1929 if (channel_created) {
1930 /* Let's not leak a useless channel. */
1931 kernel_destroy_channel(kchan);
1932 }
1933 goto error;
e5f5db7f 1934 }
6e911cad
MD
1935 break;
1936 case LTTNG_EVENT_SYSCALL:
00a62084
MD
1937 ret = event_kernel_enable_event(kchan, event,
1938 filter_expression, filter);
a969e101
MD
1939 /* We have passed ownership */
1940 filter_expression = NULL;
1941 filter = NULL;
e2b957af
MD
1942 if (ret != LTTNG_OK) {
1943 goto error;
1944 }
6e911cad
MD
1945 break;
1946 default:
1947 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
1948 goto error;
1949 }
1950
1951 kernel_wait_quiescent(kernel_tracer_fd);
1952 break;
1953 }
1954 case LTTNG_DOMAIN_UST:
1955 {
1956 struct ltt_ust_channel *uchan;
1957 struct ltt_ust_session *usess = session->ust_session;
1958
1959 assert(usess);
1960
85076754
MD
1961 /*
1962 * If a non-default channel has been created in the
1963 * session, explicitely require that -c chan_name needs
1964 * to be provided.
1965 */
1966 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1967 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1968 goto error;
1969 }
1970
2f77fc4b
DG
1971 /* Get channel from global UST domain */
1972 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1973 channel_name);
1974 if (uchan == NULL) {
1975 /* Create default channel */
0a9c6494
DG
1976 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1977 usess->buffer_type);
2f77fc4b 1978 if (attr == NULL) {
f73fabfd 1979 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1980 goto error;
1981 }
04c17253
MD
1982 if (lttng_strncpy(attr->name, channel_name,
1983 sizeof(attr->name))) {
1984 ret = LTTNG_ERR_INVALID;
04c17253
MD
1985 goto error;
1986 }
2f77fc4b
DG
1987
1988 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1989 if (ret != LTTNG_OK) {
2f77fc4b
DG
1990 goto error;
1991 }
2f77fc4b
DG
1992
1993 /* Get the newly created channel reference back */
1994 uchan = trace_ust_find_channel_by_name(
1995 usess->domain_global.channels, channel_name);
1996 assert(uchan);
1997 }
1998
141feb8c
JG
1999 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2000 /*
2001 * Don't allow users to add UST events to channels which
2002 * are assigned to a userspace subdomain (JUL, Log4J,
2003 * Python, etc.).
2004 */
2005 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2006 goto error;
2007 }
2008
dac8e046
JG
2009 if (!internal_event) {
2010 /*
2011 * Ensure the event name is not reserved for internal
2012 * use.
2013 */
2014 ret = validate_ust_event_name(event->name);
2015 if (ret) {
2016 WARN("Userspace event name %s failed validation.",
2017 event->name ?
2018 event->name : "NULL");
2019 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2020 goto error;
2021 }
2022 }
2023
2f77fc4b 2024 /* At this point, the session and channel exist on the tracer */
6b453b5e 2025 ret = event_ust_enable_tracepoint(usess, uchan, event,
88f06f15
JG
2026 filter_expression, filter, exclusion,
2027 internal_event);
49d21f93
MD
2028 /* We have passed ownership */
2029 filter_expression = NULL;
2030 filter = NULL;
2031 exclusion = NULL;
94382e15
JG
2032 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2033 goto already_enabled;
2034 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2035 goto error;
2036 }
2037 break;
2038 }
5cdb6027 2039 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2040 case LTTNG_DOMAIN_JUL:
0e115563 2041 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2042 {
da6c3a50 2043 const char *default_event_name, *default_chan_name;
fefd409b 2044 struct agent *agt;
f20baf8e
DG
2045 struct lttng_event uevent;
2046 struct lttng_domain tmp_dom;
2047 struct ltt_ust_session *usess = session->ust_session;
2048
2049 assert(usess);
2050
5cdb6027 2051 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2052 if (!agt) {
5cdb6027 2053 agt = agent_create(domain->type);
fefd409b 2054 if (!agt) {
e5b3c48c 2055 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2056 goto error;
2057 }
2058 agent_add(agt, usess->agents);
2059 }
2060
022d91ba 2061 /* Create the default tracepoint. */
996de3c7 2062 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2063 uevent.type = LTTNG_EVENT_TRACEPOINT;
2064 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
51755dc8
JG
2065 default_event_name = event_get_default_agent_ust_name(
2066 domain->type);
da6c3a50 2067 if (!default_event_name) {
e5b3c48c 2068 ret = LTTNG_ERR_FATAL;
da6c3a50 2069 goto error;
f43f95a9 2070 }
da6c3a50 2071 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2072 uevent.name[sizeof(uevent.name) - 1] = '\0';
2073
2074 /*
2075 * The domain type is changed because we are about to enable the
2076 * default channel and event for the JUL domain that are hardcoded.
2077 * This happens in the UST domain.
2078 */
2079 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2080 tmp_dom.type = LTTNG_DOMAIN_UST;
2081
0e115563
DG
2082 switch (domain->type) {
2083 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2084 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2085 break;
2086 case LTTNG_DOMAIN_JUL:
da6c3a50 2087 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2088 break;
2089 case LTTNG_DOMAIN_PYTHON:
2090 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2091 break;
2092 default:
e98a44b0 2093 /* The switch/case we are in makes this impossible */
0e115563 2094 assert(0);
da6c3a50
DG
2095 }
2096
971da06a 2097 {
8404118c 2098 char *filter_expression_copy = NULL;
51755dc8 2099 struct lttng_filter_bytecode *filter_copy = NULL;
971da06a
JG
2100
2101 if (filter) {
51755dc8
JG
2102 const size_t filter_size = sizeof(
2103 struct lttng_filter_bytecode)
2104 + filter->len;
2105
2106 filter_copy = zmalloc(filter_size);
971da06a 2107 if (!filter_copy) {
018096a4 2108 ret = LTTNG_ERR_NOMEM;
b742e3e2 2109 goto error;
971da06a 2110 }
51755dc8 2111 memcpy(filter_copy, filter, filter_size);
971da06a 2112
8404118c
JG
2113 filter_expression_copy =
2114 strdup(filter_expression);
2115 if (!filter_expression) {
2116 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2117 }
2118
2119 if (!filter_expression_copy || !filter_copy) {
2120 free(filter_expression_copy);
2121 free(filter_copy);
2122 goto error;
8404118c 2123 }
971da06a
JG
2124 }
2125
88f06f15 2126 ret = cmd_enable_event_internal(session, &tmp_dom,
971da06a 2127 (char *) default_chan_name,
8404118c
JG
2128 &uevent, filter_expression_copy,
2129 filter_copy, NULL, wpipe);
971da06a
JG
2130 }
2131
94382e15
JG
2132 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2133 goto already_enabled;
2134 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2135 goto error;
2136 }
2137
2138 /* The wild card * means that everything should be enabled. */
2139 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
8404118c
JG
2140 ret = event_agent_enable_all(usess, agt, event, filter,
2141 filter_expression);
f20baf8e 2142 } else {
8404118c
JG
2143 ret = event_agent_enable(usess, agt, event, filter,
2144 filter_expression);
f20baf8e 2145 }
51755dc8
JG
2146 filter = NULL;
2147 filter_expression = NULL;
f20baf8e
DG
2148 if (ret != LTTNG_OK) {
2149 goto error;
2150 }
2151
2152 break;
2153 }
2f77fc4b 2154 default:
f73fabfd 2155 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2156 goto error;
2157 }
2158
f73fabfd 2159 ret = LTTNG_OK;
2f77fc4b 2160
94382e15 2161already_enabled:
2f77fc4b 2162error:
49d21f93
MD
2163 free(filter_expression);
2164 free(filter);
2165 free(exclusion);
cf0bcb51 2166 channel_attr_destroy(attr);
2223c96f 2167 rcu_read_unlock();
2f77fc4b
DG
2168 return ret;
2169}
2170
88f06f15
JG
2171/*
2172 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2173 * We own filter, exclusion, and filter_expression.
2174 */
2175int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
2176 char *channel_name, struct lttng_event *event,
2177 char *filter_expression,
2178 struct lttng_filter_bytecode *filter,
2179 struct lttng_event_exclusion *exclusion,
2180 int wpipe)
2181{
2182 return _cmd_enable_event(session, domain, channel_name, event,
2183 filter_expression, filter, exclusion, wpipe, false);
2184}
2185
2186/*
2187 * Enable an event which is internal to LTTng. An internal should
2188 * never be made visible to clients and are immune to checks such as
2189 * reserved names.
2190 */
2191static int cmd_enable_event_internal(struct ltt_session *session,
2192 struct lttng_domain *domain,
2193 char *channel_name, struct lttng_event *event,
2194 char *filter_expression,
2195 struct lttng_filter_bytecode *filter,
2196 struct lttng_event_exclusion *exclusion,
2197 int wpipe)
2198{
2199 return _cmd_enable_event(session, domain, channel_name, event,
2200 filter_expression, filter, exclusion, wpipe, true);
2201}
2202
2f77fc4b
DG
2203/*
2204 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2205 */
56a37563
JG
2206ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2207 struct lttng_event **events)
2f77fc4b
DG
2208{
2209 int ret;
2210 ssize_t nb_events = 0;
2211
2212 switch (domain) {
2213 case LTTNG_DOMAIN_KERNEL:
2214 nb_events = kernel_list_events(kernel_tracer_fd, events);
2215 if (nb_events < 0) {
f73fabfd 2216 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
2217 goto error;
2218 }
2219 break;
2220 case LTTNG_DOMAIN_UST:
2221 nb_events = ust_app_list_events(events);
2222 if (nb_events < 0) {
f73fabfd 2223 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2224 goto error;
2225 }
2226 break;
5cdb6027 2227 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2228 case LTTNG_DOMAIN_JUL:
0e115563 2229 case LTTNG_DOMAIN_PYTHON:
f60140a1 2230 nb_events = agent_list_events(events, domain);
3c6a091f
DG
2231 if (nb_events < 0) {
2232 ret = LTTNG_ERR_UST_LIST_FAIL;
2233 goto error;
2234 }
2235 break;
2f77fc4b 2236 default:
f73fabfd 2237 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2238 goto error;
2239 }
2240
2241 return nb_events;
2242
2243error:
2244 /* Return negative value to differentiate return code */
2245 return -ret;
2246}
2247
2248/*
2249 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2250 */
56a37563 2251ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2f77fc4b
DG
2252 struct lttng_event_field **fields)
2253{
2254 int ret;
2255 ssize_t nb_fields = 0;
2256
2257 switch (domain) {
2258 case LTTNG_DOMAIN_UST:
2259 nb_fields = ust_app_list_event_fields(fields);
2260 if (nb_fields < 0) {
f73fabfd 2261 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2262 goto error;
2263 }
2264 break;
2265 case LTTNG_DOMAIN_KERNEL:
2266 default: /* fall-through */
f73fabfd 2267 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2268 goto error;
2269 }
2270
2271 return nb_fields;
2272
2273error:
2274 /* Return negative value to differentiate return code */
2275 return -ret;
2276}
2277
834978fd
DG
2278ssize_t cmd_list_syscalls(struct lttng_event **events)
2279{
2280 return syscall_table_list(events);
2281}
2282
a5dfbb9d
MD
2283/*
2284 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2285 *
2286 * Called with session lock held.
2287 */
2288ssize_t cmd_list_tracker_pids(struct ltt_session *session,
56a37563 2289 enum lttng_domain_type domain, int32_t **pids)
a5dfbb9d
MD
2290{
2291 int ret;
2292 ssize_t nr_pids = 0;
2293
2294 switch (domain) {
2295 case LTTNG_DOMAIN_KERNEL:
2296 {
2297 struct ltt_kernel_session *ksess;
2298
2299 ksess = session->kernel_session;
2300 nr_pids = kernel_list_tracker_pids(ksess, pids);
2301 if (nr_pids < 0) {
2302 ret = LTTNG_ERR_KERN_LIST_FAIL;
2303 goto error;
2304 }
2305 break;
2306 }
2307 case LTTNG_DOMAIN_UST:
2308 {
2309 struct ltt_ust_session *usess;
2310
2311 usess = session->ust_session;
2312 nr_pids = trace_ust_list_tracker_pids(usess, pids);
2313 if (nr_pids < 0) {
2314 ret = LTTNG_ERR_UST_LIST_FAIL;
2315 goto error;
2316 }
2317 break;
2318 }
2319 case LTTNG_DOMAIN_LOG4J:
2320 case LTTNG_DOMAIN_JUL:
2321 case LTTNG_DOMAIN_PYTHON:
2322 default:
2323 ret = LTTNG_ERR_UND;
2324 goto error;
2325 }
2326
2327 return nr_pids;
2328
2329error:
2330 /* Return negative value to differentiate return code */
2331 return -ret;
2332}
2333
2f77fc4b
DG
2334/*
2335 * Command LTTNG_START_TRACE processed by the client thread.
a9ad0c8f
MD
2336 *
2337 * Called with session mutex held.
2f77fc4b
DG
2338 */
2339int cmd_start_trace(struct ltt_session *session)
2340{
2341 int ret;
cde3e505 2342 unsigned long nb_chan = 0;
2f77fc4b
DG
2343 struct ltt_kernel_session *ksession;
2344 struct ltt_ust_session *usess;
2f77fc4b
DG
2345
2346 assert(session);
2347
2348 /* Ease our life a bit ;) */
2349 ksession = session->kernel_session;
2350 usess = session->ust_session;
2351
8382cf6f
DG
2352 /* Is the session already started? */
2353 if (session->active) {
f73fabfd 2354 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
2355 goto error;
2356 }
2357
cde3e505
DG
2358 /*
2359 * Starting a session without channel is useless since after that it's not
2360 * possible to enable channel thus inform the client.
2361 */
2362 if (usess && usess->domain_global.channels) {
2363 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2364 }
2365 if (ksession) {
2366 nb_chan += ksession->channel_count;
2367 }
2368 if (!nb_chan) {
2369 ret = LTTNG_ERR_NO_CHANNEL;
2370 goto error;
2371 }
2372
2f77fc4b
DG
2373 /* Kernel tracing */
2374 if (ksession != NULL) {
9b6c7ec5
DG
2375 ret = start_kernel_session(ksession, kernel_tracer_fd);
2376 if (ret != LTTNG_OK) {
2f77fc4b
DG
2377 goto error;
2378 }
2f77fc4b
DG
2379 }
2380
2381 /* Flag session that trace should start automatically */
2382 if (usess) {
14fb1ebe
DG
2383 /*
2384 * Even though the start trace might fail, flag this session active so
2385 * other application coming in are started by default.
2386 */
2387 usess->active = 1;
2f77fc4b
DG
2388
2389 ret = ust_app_start_trace_all(usess);
2390 if (ret < 0) {
f73fabfd 2391 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
2392 goto error;
2393 }
2394 }
2395
8382cf6f
DG
2396 /* Flag this after a successful start. */
2397 session->has_been_started = 1;
2398 session->active = 1;
9b6c7ec5 2399
f73fabfd 2400 ret = LTTNG_OK;
2f77fc4b
DG
2401
2402error:
2403 return ret;
2404}
2405
2406/*
2407 * Command LTTNG_STOP_TRACE processed by the client thread.
2408 */
2409int cmd_stop_trace(struct ltt_session *session)
2410{
2411 int ret;
2412 struct ltt_kernel_channel *kchan;
2413 struct ltt_kernel_session *ksession;
2414 struct ltt_ust_session *usess;
2415
2416 assert(session);
2417
2418 /* Short cut */
2419 ksession = session->kernel_session;
2420 usess = session->ust_session;
2421
8382cf6f
DG
2422 /* Session is not active. Skip everythong and inform the client. */
2423 if (!session->active) {
f73fabfd 2424 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
2425 goto error;
2426 }
2427
2f77fc4b 2428 /* Kernel tracer */
14fb1ebe 2429 if (ksession && ksession->active) {
2f77fc4b
DG
2430 DBG("Stop kernel tracing");
2431
566d8970
MD
2432 ret = kernel_stop_session(ksession);
2433 if (ret < 0) {
2434 ret = LTTNG_ERR_KERN_STOP_FAIL;
2435 goto error;
2436 }
2437
2438 kernel_wait_quiescent(kernel_tracer_fd);
2439
2440 /* Flush metadata after stopping (if exists) */
2f77fc4b
DG
2441 if (ksession->metadata_stream_fd >= 0) {
2442 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
2443 if (ret < 0) {
2444 ERR("Kernel metadata flush failed");
2445 }
2446 }
2447
566d8970 2448 /* Flush all buffers after stopping */
2f77fc4b
DG
2449 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2450 ret = kernel_flush_buffer(kchan);
2451 if (ret < 0) {
2452 ERR("Kernel flush buffer error");
2453 }
2454 }
2455
14fb1ebe 2456 ksession->active = 0;
2f77fc4b
DG
2457 }
2458
14fb1ebe
DG
2459 if (usess && usess->active) {
2460 /*
2461 * Even though the stop trace might fail, flag this session inactive so
2462 * other application coming in are not started by default.
2463 */
2464 usess->active = 0;
2f77fc4b
DG
2465
2466 ret = ust_app_stop_trace_all(usess);
2467 if (ret < 0) {
f73fabfd 2468 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2469 goto error;
2470 }
2471 }
2472
8382cf6f
DG
2473 /* Flag inactive after a successful stop. */
2474 session->active = 0;
f73fabfd 2475 ret = LTTNG_OK;
2f77fc4b
DG
2476
2477error:
2478 return ret;
2479}
2480
2481/*
2482 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2483 */
bda32d56
JG
2484int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2485 struct lttng_uri *uris)
2f77fc4b
DG
2486{
2487 int ret, i;
2488 struct ltt_kernel_session *ksess = session->kernel_session;
2489 struct ltt_ust_session *usess = session->ust_session;
2f77fc4b
DG
2490
2491 assert(session);
2492 assert(uris);
2493 assert(nb_uri > 0);
2494
8382cf6f
DG
2495 /* Can't set consumer URI if the session is active. */
2496 if (session->active) {
f73fabfd 2497 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
2498 goto error;
2499 }
2500
bda32d56 2501 /* Set the "global" consumer URIs */
2f77fc4b 2502 for (i = 0; i < nb_uri; i++) {
bda32d56
JG
2503 ret = add_uri_to_consumer(session->consumer,
2504 &uris[i], 0, session->name);
a74934ba 2505 if (ret != LTTNG_OK) {
2f77fc4b
DG
2506 goto error;
2507 }
2f77fc4b
DG
2508 }
2509
bda32d56
JG
2510 /* Set UST session URIs */
2511 if (session->ust_session) {
2512 for (i = 0; i < nb_uri; i++) {
2513 ret = add_uri_to_consumer(
2514 session->ust_session->consumer,
2515 &uris[i], LTTNG_DOMAIN_UST,
2516 session->name);
2517 if (ret != LTTNG_OK) {
2518 goto error;
2519 }
2520 }
2521 }
2522
2523 /* Set kernel session URIs */
2524 if (session->kernel_session) {
2525 for (i = 0; i < nb_uri; i++) {
2526 ret = add_uri_to_consumer(
2527 session->kernel_session->consumer,
2528 &uris[i], LTTNG_DOMAIN_KERNEL,
2529 session->name);
2530 if (ret != LTTNG_OK) {
2531 goto error;
2532 }
2533 }
2534 }
2535
7ab70fe0
DG
2536 /*
2537 * Make sure to set the session in output mode after we set URI since a
2538 * session can be created without URL (thus flagged in no output mode).
2539 */
2540 session->output_traces = 1;
2541 if (ksess) {
2542 ksess->output_traces = 1;
bda32d56
JG
2543 }
2544
2545 if (usess) {
7ab70fe0
DG
2546 usess->output_traces = 1;
2547 }
2548
2f77fc4b 2549 /* All good! */
f73fabfd 2550 ret = LTTNG_OK;
2f77fc4b
DG
2551
2552error:
2553 return ret;
2554}
2555
2556/*
2557 * Command LTTNG_CREATE_SESSION processed by the client thread.
2558 */
2559int cmd_create_session_uri(char *name, struct lttng_uri *uris,
ecc48a90 2560 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2f77fc4b
DG
2561{
2562 int ret;
2f77fc4b
DG
2563 struct ltt_session *session;
2564
2565 assert(name);
2bba9e53 2566 assert(creds);
785d2d0d 2567
2f77fc4b
DG
2568 /*
2569 * Verify if the session already exist
2570 *
2571 * XXX: There is no need for the session lock list here since the caller
2572 * (process_client_msg) is holding it. We might want to change that so a
2573 * single command does not lock the entire session list.
2574 */
2575 session = session_find_by_name(name);
2576 if (session != NULL) {
f73fabfd 2577 ret = LTTNG_ERR_EXIST_SESS;
2f77fc4b
DG
2578 goto find_error;
2579 }
2580
2581 /* Create tracing session in the registry */
dec56f6c 2582 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2f77fc4b 2583 LTTNG_SOCK_GET_GID_CRED(creds));
f73fabfd 2584 if (ret != LTTNG_OK) {
2f77fc4b
DG
2585 goto session_error;
2586 }
2587
2588 /*
2589 * Get the newly created session pointer back
2590 *
2591 * XXX: There is no need for the session lock list here since the caller
2592 * (process_client_msg) is holding it. We might want to change that so a
2593 * single command does not lock the entire session list.
2594 */
2595 session = session_find_by_name(name);
2596 assert(session);
2597
ecc48a90 2598 session->live_timer = live_timer;
2f77fc4b
DG
2599 /* Create default consumer output for the session not yet created. */
2600 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2601 if (session->consumer == NULL) {
f73fabfd 2602 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2603 goto consumer_error;
2604 }
2605
2bba9e53 2606 if (uris) {
bda32d56 2607 ret = cmd_set_consumer_uri(session, nb_uri, uris);
2bba9e53
DG
2608 if (ret != LTTNG_OK) {
2609 goto consumer_error;
2610 }
2611 session->output_traces = 1;
2612 } else {
2613 session->output_traces = 0;
2614 DBG2("Session %s created with no output", session->name);
2f77fc4b
DG
2615 }
2616
2617 session->consumer->enabled = 1;
2618
f73fabfd 2619 return LTTNG_OK;
2f77fc4b
DG
2620
2621consumer_error:
2622 session_destroy(session);
2623session_error:
2624find_error:
2625 return ret;
2626}
2627
27babd3a
DG
2628/*
2629 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2630 */
2631int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2632 size_t nb_uri, lttng_sock_cred *creds)
2633{
2634 int ret;
2635 struct ltt_session *session;
2636 struct snapshot_output *new_output = NULL;
2637
2638 assert(name);
2639 assert(creds);
2640
2641 /*
2642 * Create session in no output mode with URIs set to NULL. The uris we've
2643 * received are for a default snapshot output if one.
2644 */
ae58b817 2645 ret = cmd_create_session_uri(name, NULL, 0, creds, 0);
27babd3a
DG
2646 if (ret != LTTNG_OK) {
2647 goto error;
2648 }
2649
2650 /* Get the newly created session pointer back. This should NEVER fail. */
2651 session = session_find_by_name(name);
2652 assert(session);
2653
2654 /* Flag session for snapshot mode. */
2655 session->snapshot_mode = 1;
2656
2657 /* Skip snapshot output creation if no URI is given. */
2658 if (nb_uri == 0) {
2659 goto end;
2660 }
2661
2662 new_output = snapshot_output_alloc();
2663 if (!new_output) {
2664 ret = LTTNG_ERR_NOMEM;
2665 goto error_snapshot_alloc;
2666 }
2667
2668 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2669 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2670 if (ret < 0) {
2671 if (ret == -ENOMEM) {
2672 ret = LTTNG_ERR_NOMEM;
2673 } else {
2674 ret = LTTNG_ERR_INVALID;
2675 }
2676 goto error_snapshot;
2677 }
2678
2679 rcu_read_lock();
2680 snapshot_add_output(&session->snapshot, new_output);
2681 rcu_read_unlock();
2682
2683end:
2684 return LTTNG_OK;
2685
2686error_snapshot:
2687 snapshot_output_destroy(new_output);
2688error_snapshot_alloc:
2689 session_destroy(session);
2690error:
2691 return ret;
2692}
2693
2f77fc4b
DG
2694/*
2695 * Command LTTNG_DESTROY_SESSION processed by the client thread.
a9ad0c8f
MD
2696 *
2697 * Called with session lock held.
2f77fc4b
DG
2698 */
2699int cmd_destroy_session(struct ltt_session *session, int wpipe)
2700{
2701 int ret;
2702 struct ltt_ust_session *usess;
2703 struct ltt_kernel_session *ksess;
2704
2705 /* Safety net */
2706 assert(session);
2707
2708 usess = session->ust_session;
2709 ksess = session->kernel_session;
2710
2711 /* Clean kernel session teardown */
2712 kernel_destroy_session(ksess);
2713
2714 /* UST session teardown */
2715 if (usess) {
2716 /* Close any relayd session */
2717 consumer_output_send_destroy_relayd(usess->consumer);
2718
2719 /* Destroy every UST application related to this session. */
2720 ret = ust_app_destroy_trace_all(usess);
2721 if (ret) {
2722 ERR("Error in ust_app_destroy_trace_all");
2723 }
2724
2725 /* Clean up the rest. */
2726 trace_ust_destroy_session(usess);
2727 }
2728
2729 /*
2730 * Must notify the kernel thread here to update it's poll set in order to
2731 * remove the channel(s)' fd just destroyed.
2732 */
2733 ret = notify_thread_pipe(wpipe);
2734 if (ret < 0) {
2735 PERROR("write kernel poll pipe");
2736 }
2737
2738 ret = session_destroy(session);
2739
2740 return ret;
2741}
2742
2f77fc4b
DG
2743/*
2744 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2745 */
56a37563
JG
2746int cmd_register_consumer(struct ltt_session *session,
2747 enum lttng_domain_type domain, const char *sock_path,
2748 struct consumer_data *cdata)
2f77fc4b
DG
2749{
2750 int ret, sock;
dd81b457 2751 struct consumer_socket *socket = NULL;
2f77fc4b
DG
2752
2753 assert(session);
2754 assert(cdata);
2755 assert(sock_path);
2756
2757 switch (domain) {
2758 case LTTNG_DOMAIN_KERNEL:
2759 {
2760 struct ltt_kernel_session *ksess = session->kernel_session;
2761
2762 assert(ksess);
2763
2764 /* Can't register a consumer if there is already one */
2765 if (ksess->consumer_fds_sent != 0) {
f73fabfd 2766 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
2767 goto error;
2768 }
2769
2770 sock = lttcomm_connect_unix_sock(sock_path);
2771 if (sock < 0) {
f73fabfd 2772 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
2773 goto error;
2774 }
4ce514c4 2775 cdata->cmd_sock = sock;
2f77fc4b 2776
4ce514c4 2777 socket = consumer_allocate_socket(&cdata->cmd_sock);
2f77fc4b 2778 if (socket == NULL) {
f66c074c
DG
2779 ret = close(sock);
2780 if (ret < 0) {
2781 PERROR("close register consumer");
2782 }
4ce514c4 2783 cdata->cmd_sock = -1;
f73fabfd 2784 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2785 goto error;
2786 }
2787
2788 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2789 if (socket->lock == NULL) {
2790 PERROR("zmalloc pthread mutex");
f73fabfd 2791 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2792 goto error;
2793 }
2794 pthread_mutex_init(socket->lock, NULL);
2795 socket->registered = 1;
2796
2797 rcu_read_lock();
2798 consumer_add_socket(socket, ksess->consumer);
2799 rcu_read_unlock();
2800
2801 pthread_mutex_lock(&cdata->pid_mutex);
2802 cdata->pid = -1;
2803 pthread_mutex_unlock(&cdata->pid_mutex);
2804
2805 break;
2806 }
2807 default:
2808 /* TODO: Userspace tracing */
f73fabfd 2809 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2810 goto error;
2811 }
2812
dd81b457 2813 return LTTNG_OK;
2f77fc4b
DG
2814
2815error:
dd81b457
DG
2816 if (socket) {
2817 consumer_destroy_socket(socket);
2818 }
2f77fc4b
DG
2819 return ret;
2820}
2821
2822/*
2823 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2824 */
2825ssize_t cmd_list_domains(struct ltt_session *session,
2826 struct lttng_domain **domains)
2827{
2828 int ret, index = 0;
2829 ssize_t nb_dom = 0;
fefd409b
DG
2830 struct agent *agt;
2831 struct lttng_ht_iter iter;
2f77fc4b
DG
2832
2833 if (session->kernel_session != NULL) {
2834 DBG3("Listing domains found kernel domain");
2835 nb_dom++;
2836 }
2837
2838 if (session->ust_session != NULL) {
2839 DBG3("Listing domains found UST global domain");
2840 nb_dom++;
3c6a091f 2841
e0a74f01 2842 rcu_read_lock();
fefd409b
DG
2843 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2844 agt, node.node) {
2845 if (agt->being_used) {
2846 nb_dom++;
2847 }
3c6a091f 2848 }
e0a74f01 2849 rcu_read_unlock();
2f77fc4b
DG
2850 }
2851
fa64dfb4
JG
2852 if (!nb_dom) {
2853 goto end;
2854 }
2855
2f77fc4b
DG
2856 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2857 if (*domains == NULL) {
f73fabfd 2858 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2859 goto error;
2860 }
2861
2862 if (session->kernel_session != NULL) {
2863 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
b5edb9e8
PP
2864
2865 /* Kernel session buffer type is always GLOBAL */
2866 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
2867
2f77fc4b
DG
2868 index++;
2869 }
2870
2871 if (session->ust_session != NULL) {
2872 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 2873 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b 2874 index++;
3c6a091f 2875
e0a74f01 2876 rcu_read_lock();
fefd409b
DG
2877 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2878 agt, node.node) {
2879 if (agt->being_used) {
2880 (*domains)[index].type = agt->domain;
2881 (*domains)[index].buf_type = session->ust_session->buffer_type;
2882 index++;
2883 }
3c6a091f 2884 }
e0a74f01 2885 rcu_read_unlock();
2f77fc4b 2886 }
fa64dfb4 2887end:
2f77fc4b
DG
2888 return nb_dom;
2889
2890error:
f73fabfd
DG
2891 /* Return negative value to differentiate return code */
2892 return -ret;
2f77fc4b
DG
2893}
2894
2895
2896/*
2897 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2898 */
56a37563
JG
2899ssize_t cmd_list_channels(enum lttng_domain_type domain,
2900 struct ltt_session *session, struct lttng_channel **channels)
2f77fc4b 2901{
53e367f9 2902 ssize_t nb_chan = 0, payload_size = 0, ret;
2f77fc4b
DG
2903
2904 switch (domain) {
2905 case LTTNG_DOMAIN_KERNEL:
2906 if (session->kernel_session != NULL) {
2907 nb_chan = session->kernel_session->channel_count;
2908 }
2909 DBG3("Number of kernel channels %zd", nb_chan);
c7d620a2 2910 if (nb_chan <= 0) {
53e367f9
JG
2911 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2912 goto end;
c7d620a2 2913 }
2f77fc4b
DG
2914 break;
2915 case LTTNG_DOMAIN_UST:
2916 if (session->ust_session != NULL) {
7ffc31a2 2917 rcu_read_lock();
2f77fc4b 2918 nb_chan = lttng_ht_get_count(
7ffc31a2
JG
2919 session->ust_session->domain_global.channels);
2920 rcu_read_unlock();
2f77fc4b
DG
2921 }
2922 DBG3("Number of UST global channels %zd", nb_chan);
ade7ce52 2923 if (nb_chan < 0) {
53e367f9
JG
2924 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
2925 goto end;
c7d620a2 2926 }
2f77fc4b
DG
2927 break;
2928 default:
53e367f9
JG
2929 ret = -LTTNG_ERR_UND;
2930 goto end;
2f77fc4b
DG
2931 }
2932
2933 if (nb_chan > 0) {
53e367f9 2934 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51
JG
2935 sizeof(struct lttng_channel_extended);
2936 struct lttng_channel_extended *channel_exts;
53e367f9
JG
2937
2938 payload_size = nb_chan * channel_size;
2939 *channels = zmalloc(payload_size);
2f77fc4b 2940 if (*channels == NULL) {
53e367f9
JG
2941 ret = -LTTNG_ERR_FATAL;
2942 goto end;
2f77fc4b
DG
2943 }
2944
53e367f9
JG
2945 channel_exts = ((void *) *channels) +
2946 (nb_chan * sizeof(struct lttng_channel));
d449df4a
MD
2947 ret = list_lttng_channels(domain, session, *channels, channel_exts);
2948 if (ret != LTTNG_OK) {
2949 free(*channels);
2950 *channels = NULL;
2951 goto end;
2952 }
53e367f9
JG
2953 } else {
2954 *channels = NULL;
2f77fc4b
DG
2955 }
2956
53e367f9
JG
2957 ret = payload_size;
2958end:
2959 return ret;
2f77fc4b
DG
2960}
2961
2962/*
2963 * Command LTTNG_LIST_EVENTS processed by the client thread.
2964 */
56a37563
JG
2965ssize_t cmd_list_events(enum lttng_domain_type domain,
2966 struct ltt_session *session, char *channel_name,
b4e3ceb9 2967 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
2968{
2969 int ret = 0;
2970 ssize_t nb_event = 0;
2971
2972 switch (domain) {
2973 case LTTNG_DOMAIN_KERNEL:
2974 if (session->kernel_session != NULL) {
2975 nb_event = list_lttng_kernel_events(channel_name,
b4e3ceb9
PP
2976 session->kernel_session, events,
2977 total_size);
2f77fc4b
DG
2978 }
2979 break;
2980 case LTTNG_DOMAIN_UST:
2981 {
2982 if (session->ust_session != NULL) {
2983 nb_event = list_lttng_ust_global_events(channel_name,
b4e3ceb9
PP
2984 &session->ust_session->domain_global, events,
2985 total_size);
2f77fc4b
DG
2986 }
2987 break;
2988 }
5cdb6027 2989 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2990 case LTTNG_DOMAIN_JUL:
0e115563 2991 case LTTNG_DOMAIN_PYTHON:
3c6a091f 2992 if (session->ust_session) {
fefd409b
DG
2993 struct lttng_ht_iter iter;
2994 struct agent *agt;
2995
b11feea5 2996 rcu_read_lock();
fefd409b
DG
2997 cds_lfht_for_each_entry(session->ust_session->agents->ht,
2998 &iter.iter, agt, node.node) {
1dfd9906
JG
2999 if (agt->domain == domain) {
3000 nb_event = list_lttng_agent_events(
b4e3ceb9
PP
3001 agt, events,
3002 total_size);
1dfd9906
JG
3003 break;
3004 }
fefd409b 3005 }
b11feea5 3006 rcu_read_unlock();
3c6a091f
DG
3007 }
3008 break;
2f77fc4b 3009 default:
f73fabfd 3010 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3011 goto error;
3012 }
3013
f73fabfd 3014 return nb_event;
2f77fc4b
DG
3015
3016error:
f73fabfd
DG
3017 /* Return negative value to differentiate return code */
3018 return -ret;
2f77fc4b
DG
3019}
3020
3021/*
3022 * Using the session list, filled a lttng_session array to send back to the
3023 * client for session listing.
3024 *
3025 * The session list lock MUST be acquired before calling this function. Use
3026 * session_lock_list() and session_unlock_list().
3027 */
3028void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
3029 gid_t gid)
3030{
3031 int ret;
3032 unsigned int i = 0;
3033 struct ltt_session *session;
3034 struct ltt_session_list *list = session_get_list();
3035
3036 DBG("Getting all available session for UID %d GID %d",
3037 uid, gid);
3038 /*
3039 * Iterate over session list and append data after the control struct in
3040 * the buffer.
3041 */
3042 cds_list_for_each_entry(session, &list->head, list) {
3043 /*
3044 * Only list the sessions the user can control.
3045 */
3046 if (!session_access_ok(session, uid, gid)) {
3047 continue;
3048 }
3049
3050 struct ltt_kernel_session *ksess = session->kernel_session;
3051 struct ltt_ust_session *usess = session->ust_session;
3052
3053 if (session->consumer->type == CONSUMER_DST_NET ||
3054 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3055 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3056 ret = build_network_session_path(sessions[i].path,
dec56f6c 3057 sizeof(sessions[i].path), session);
2f77fc4b 3058 } else {
dec56f6c 3059 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2f77fc4b
DG
3060 session->consumer->dst.trace_path);
3061 }
3062 if (ret < 0) {
3063 PERROR("snprintf session path");
3064 continue;
3065 }
3066
3067 strncpy(sessions[i].name, session->name, NAME_MAX);
3068 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 3069 sessions[i].enabled = session->active;
2cbf8fed 3070 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 3071 sessions[i].live_timer_interval = session->live_timer;
2f77fc4b
DG
3072 i++;
3073 }
3074}
3075
806e2684 3076/*
6d805429 3077 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 3078 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 3079 */
6d805429 3080int cmd_data_pending(struct ltt_session *session)
806e2684
DG
3081{
3082 int ret;
3083 struct ltt_kernel_session *ksess = session->kernel_session;
3084 struct ltt_ust_session *usess = session->ust_session;
3085
3086 assert(session);
3087
3088 /* Session MUST be stopped to ask for data availability. */
8382cf6f 3089 if (session->active) {
806e2684
DG
3090 ret = LTTNG_ERR_SESSION_STARTED;
3091 goto error;
3a89d11a
DG
3092 } else {
3093 /*
3094 * If stopped, just make sure we've started before else the above call
3095 * will always send that there is data pending.
3096 *
3097 * The consumer assumes that when the data pending command is received,
3098 * the trace has been started before or else no output data is written
3099 * by the streams which is a condition for data pending. So, this is
3100 * *VERY* important that we don't ask the consumer before a start
3101 * trace.
3102 */
8382cf6f 3103 if (!session->has_been_started) {
3a89d11a
DG
3104 ret = 0;
3105 goto error;
3106 }
806e2684
DG
3107 }
3108
3109 if (ksess && ksess->consumer) {
6d805429
DG
3110 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3111 if (ret == 1) {
806e2684
DG
3112 /* Data is still being extracted for the kernel. */
3113 goto error;
3114 }
3115 }
3116
3117 if (usess && usess->consumer) {
6d805429
DG
3118 ret = consumer_is_data_pending(usess->id, usess->consumer);
3119 if (ret == 1) {
806e2684
DG
3120 /* Data is still being extracted for the kernel. */
3121 goto error;
3122 }
3123 }
3124
3125 /* Data is ready to be read by a viewer */
6d805429 3126 ret = 0;
806e2684
DG
3127
3128error:
3129 return ret;
3130}
3131
6dc3064a
DG
3132/*
3133 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3134 *
3135 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3136 */
3137int cmd_snapshot_add_output(struct ltt_session *session,
3138 struct lttng_snapshot_output *output, uint32_t *id)
3139{
3140 int ret;
3141 struct snapshot_output *new_output;
3142
3143 assert(session);
3144 assert(output);
3145
3146 DBG("Cmd snapshot add output for session %s", session->name);
3147
3148 /*
903ef685 3149 * Can't create an output if the session is not set in no-output mode.
6dc3064a
DG
3150 */
3151 if (session->output_traces) {
903ef685 3152 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3153 goto error;
3154 }
3155
3156 /* Only one output is allowed until we have the "tee" feature. */
3157 if (session->snapshot.nb_output == 1) {
3158 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3159 goto error;
3160 }
3161
3162 new_output = snapshot_output_alloc();
3163 if (!new_output) {
3164 ret = LTTNG_ERR_NOMEM;
3165 goto error;
3166 }
3167
3168 ret = snapshot_output_init(output->max_size, output->name,
3169 output->ctrl_url, output->data_url, session->consumer, new_output,
3170 &session->snapshot);
3171 if (ret < 0) {
3172 if (ret == -ENOMEM) {
3173 ret = LTTNG_ERR_NOMEM;
3174 } else {
3175 ret = LTTNG_ERR_INVALID;
3176 }
3177 goto free_error;
3178 }
3179
6dc3064a
DG
3180 rcu_read_lock();
3181 snapshot_add_output(&session->snapshot, new_output);
3182 if (id) {
3183 *id = new_output->id;
3184 }
3185 rcu_read_unlock();
3186
3187 return LTTNG_OK;
3188
3189free_error:
3190 snapshot_output_destroy(new_output);
3191error:
3192 return ret;
3193}
3194
3195/*
3196 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3197 *
3198 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3199 */
3200int cmd_snapshot_del_output(struct ltt_session *session,
3201 struct lttng_snapshot_output *output)
3202{
3203 int ret;
eb240553 3204 struct snapshot_output *sout = NULL;
6dc3064a
DG
3205
3206 assert(session);
3207 assert(output);
3208
6dc3064a
DG
3209 rcu_read_lock();
3210
3211 /*
d3f14b8a
MD
3212 * Permission denied to create an output if the session is not
3213 * set in no output mode.
6dc3064a
DG
3214 */
3215 if (session->output_traces) {
903ef685 3216 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3217 goto error;
3218 }
3219
eb240553
DG
3220 if (output->id) {
3221 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3222 session->name);
3223 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3224 } else if (*output->name != '\0') {
3225 DBG("Cmd snapshot del output name %s for session %s", output->name,
3226 session->name);
3227 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3228 }
6dc3064a
DG
3229 if (!sout) {
3230 ret = LTTNG_ERR_INVALID;
3231 goto error;
3232 }
3233
3234 snapshot_delete_output(&session->snapshot, sout);
3235 snapshot_output_destroy(sout);
3236 ret = LTTNG_OK;
3237
3238error:
3239 rcu_read_unlock();
3240 return ret;
3241}
3242
3243/*
3244 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3245 *
3246 * If no output is available, outputs is untouched and 0 is returned.
3247 *
3248 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3249 */
3250ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3251 struct lttng_snapshot_output **outputs)
3252{
3253 int ret, idx = 0;
b223ca94 3254 struct lttng_snapshot_output *list = NULL;
6dc3064a
DG
3255 struct lttng_ht_iter iter;
3256 struct snapshot_output *output;
3257
3258 assert(session);
3259 assert(outputs);
3260
3261 DBG("Cmd snapshot list outputs for session %s", session->name);
3262
3263 /*
d3f14b8a
MD
3264 * Permission denied to create an output if the session is not
3265 * set in no output mode.
6dc3064a
DG
3266 */
3267 if (session->output_traces) {
903ef685
JG
3268 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3269 goto end;
6dc3064a
DG
3270 }
3271
3272 if (session->snapshot.nb_output == 0) {
3273 ret = 0;
903ef685 3274 goto end;
6dc3064a
DG
3275 }
3276
3277 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
3278 if (!list) {
b223ca94 3279 ret = -LTTNG_ERR_NOMEM;
903ef685 3280 goto end;
6dc3064a
DG
3281 }
3282
3283 /* Copy list from session to the new list object. */
b223ca94 3284 rcu_read_lock();
6dc3064a
DG
3285 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
3286 output, node.node) {
3287 assert(output->consumer);
3288 list[idx].id = output->id;
3289 list[idx].max_size = output->max_size;
6ce22875
MD
3290 if (lttng_strncpy(list[idx].name, output->name,
3291 sizeof(list[idx].name))) {
3292 ret = -LTTNG_ERR_INVALID;
903ef685 3293 goto error;
6ce22875 3294 }
6dc3064a 3295 if (output->consumer->type == CONSUMER_DST_LOCAL) {
6ce22875
MD
3296 if (lttng_strncpy(list[idx].ctrl_url,
3297 output->consumer->dst.trace_path,
3298 sizeof(list[idx].ctrl_url))) {
3299 ret = -LTTNG_ERR_INVALID;
903ef685 3300 goto error;
6ce22875 3301 }
6dc3064a
DG
3302 } else {
3303 /* Control URI. */
3304 ret = uri_to_str_url(&output->consumer->dst.net.control,
3305 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
3306 if (ret < 0) {
b223ca94 3307 ret = -LTTNG_ERR_NOMEM;
903ef685 3308 goto error;
6dc3064a
DG
3309 }
3310
3311 /* Data URI. */
3312 ret = uri_to_str_url(&output->consumer->dst.net.data,
3313 list[idx].data_url, sizeof(list[idx].data_url));
3314 if (ret < 0) {
b223ca94 3315 ret = -LTTNG_ERR_NOMEM;
903ef685 3316 goto error;
6dc3064a
DG
3317 }
3318 }
3319 idx++;
3320 }
3321
3322 *outputs = list;
b223ca94
JG
3323 list = NULL;
3324 ret = session->snapshot.nb_output;
6dc3064a 3325error:
903ef685 3326 rcu_read_unlock();
b223ca94 3327 free(list);
903ef685 3328end:
b223ca94 3329 return ret;
6dc3064a
DG
3330}
3331
93ec662e
JD
3332/*
3333 * Check if we can regenerate the metadata for this session.
3334 * Only kernel, UST per-uid and non-live sessions are supported.
3335 *
3336 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3337 */
3338static
eded6438 3339int check_regenerate_metadata_support(struct ltt_session *session)
93ec662e
JD
3340{
3341 int ret;
3342
3343 assert(session);
3344
3345 if (session->live_timer != 0) {
3346 ret = LTTNG_ERR_LIVE_SESSION;
3347 goto end;
3348 }
3349 if (!session->active) {
3350 ret = LTTNG_ERR_SESSION_NOT_STARTED;
3351 goto end;
3352 }
3353 if (session->ust_session) {
3354 switch (session->ust_session->buffer_type) {
3355 case LTTNG_BUFFER_PER_UID:
3356 break;
3357 case LTTNG_BUFFER_PER_PID:
3358 ret = LTTNG_ERR_PER_PID_SESSION;
3359 goto end;
3360 default:
3361 assert(0);
3362 ret = LTTNG_ERR_UNK;
3363 goto end;
3364 }
3365 }
3366 if (session->consumer->type == CONSUMER_DST_NET &&
3367 session->consumer->relay_minor_version < 8) {
3368 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
3369 goto end;
3370 }
3371 ret = 0;
3372
3373end:
3374 return ret;
3375}
3376
7802bae3
LL
3377static
3378int clear_metadata_file(int fd)
3379{
3380 int ret;
3381
3382 ret = lseek(fd, 0, SEEK_SET);
3383 if (ret < 0) {
3384 PERROR("lseek");
3385 goto end;
3386 }
3387
3388 ret = ftruncate(fd, 0);
3389 if (ret < 0) {
3390 PERROR("ftruncate");
3391 goto end;
3392 }
3393
3394end:
3395 return ret;
3396}
3397
93ec662e 3398static
eded6438 3399int ust_regenerate_metadata(struct ltt_ust_session *usess)
93ec662e
JD
3400{
3401 int ret = 0;
3402 struct buffer_reg_uid *uid_reg = NULL;
3403 struct buffer_reg_session *session_reg = NULL;
3404
3405 rcu_read_lock();
3406 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
3407 struct ust_registry_session *registry;
3408 struct ust_registry_channel *chan;
3409 struct lttng_ht_iter iter_chan;
3410
3411 session_reg = uid_reg->registry;
3412 registry = session_reg->reg.ust;
3413
3414 pthread_mutex_lock(&registry->lock);
3415 registry->metadata_len_sent = 0;
3416 memset(registry->metadata, 0, registry->metadata_alloc_len);
3417 registry->metadata_len = 0;
3418 registry->metadata_version++;
7802bae3
LL
3419 if (registry->metadata_fd > 0) {
3420 /* Clear the metadata file's content. */
3421 ret = clear_metadata_file(registry->metadata_fd);
3422 if (ret) {
3423 pthread_mutex_unlock(&registry->lock);
3424 goto end;
3425 }
3426 }
3427
93ec662e
JD
3428 ret = ust_metadata_session_statedump(registry, NULL,
3429 registry->major, registry->minor);
3430 if (ret) {
3431 pthread_mutex_unlock(&registry->lock);
3432 ERR("Failed to generate session metadata (err = %d)",
3433 ret);
3434 goto end;
3435 }
3436 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
3437 chan, node.node) {
3438 struct ust_registry_event *event;
3439 struct lttng_ht_iter iter_event;
3440
3441 ret = ust_metadata_channel_statedump(registry, chan);
3442 if (ret) {
3443 pthread_mutex_unlock(&registry->lock);
3444 ERR("Failed to generate channel metadata "
3445 "(err = %d)", ret);
3446 goto end;
3447 }
3448 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
3449 event, node.node) {
3450 ret = ust_metadata_event_statedump(registry,
3451 chan, event);
3452 if (ret) {
3453 pthread_mutex_unlock(&registry->lock);
3454 ERR("Failed to generate event metadata "
3455 "(err = %d)", ret);
3456 goto end;
3457 }
3458 }
3459 }
3460 pthread_mutex_unlock(&registry->lock);
3461 }
3462
3463end:
3464 rcu_read_unlock();
3465 return ret;
3466}
3467
3468/*
eded6438 3469 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
93ec662e
JD
3470 *
3471 * Ask the consumer to truncate the existing metadata file(s) and
3472 * then regenerate the metadata. Live and per-pid sessions are not
3473 * supported and return an error.
3474 *
3475 * Return 0 on success or else a LTTNG_ERR code.
3476 */
eded6438 3477int cmd_regenerate_metadata(struct ltt_session *session)
93ec662e
JD
3478{
3479 int ret;
3480
3481 assert(session);
3482
eded6438 3483 ret = check_regenerate_metadata_support(session);
93ec662e
JD
3484 if (ret) {
3485 goto end;
3486 }
3487
3488 if (session->kernel_session) {
eded6438 3489 ret = kernctl_session_regenerate_metadata(
93ec662e
JD
3490 session->kernel_session->fd);
3491 if (ret < 0) {
3492 ERR("Failed to regenerate the kernel metadata");
3493 goto end;
3494 }
3495 }
3496
3497 if (session->ust_session) {
eded6438 3498 ret = ust_regenerate_metadata(session->ust_session);
93ec662e
JD
3499 if (ret < 0) {
3500 ERR("Failed to regenerate the UST metadata");
3501 goto end;
3502 }
3503 }
3504 DBG("Cmd metadata regenerate for session %s", session->name);
3505 ret = LTTNG_OK;
3506
3507end:
3508 return ret;
3509}
3510
c2561365
JD
3511/*
3512 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3513 *
3514 * Ask the tracer to regenerate a new statedump.
3515 *
3516 * Return 0 on success or else a LTTNG_ERR code.
3517 */
3518int cmd_regenerate_statedump(struct ltt_session *session)
3519{
3520 int ret;
3521
3522 assert(session);
3523
3524 if (!session->active) {
3525 ret = LTTNG_ERR_SESSION_NOT_STARTED;
3526 goto end;
3527 }
3528 ret = 0;
3529
3530 if (session->kernel_session) {
3531 ret = kernctl_session_regenerate_statedump(
3532 session->kernel_session->fd);
3533 /*
3534 * Currently, the statedump in kernel can only fail if out
3535 * of memory.
3536 */
3537 if (ret < 0) {
3538 if (ret == -ENOMEM) {
3539 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
3540 } else {
3541 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
3542 }
3543 ERR("Failed to regenerate the kernel statedump");
3544 goto end;
3545 }
3546 }
3547
3548 if (session->ust_session) {
3549 ret = ust_app_regenerate_statedump_all(session->ust_session);
3550 /*
3551 * Currently, the statedump in UST always returns 0.
3552 */
3553 if (ret < 0) {
3554 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
3555 ERR("Failed to regenerate the UST statedump");
3556 goto end;
3557 }
3558 }
3559 DBG("Cmd regenerate statedump for session %s", session->name);
3560 ret = LTTNG_OK;
3561
3562end:
3563 return ret;
3564}
3565
b0880ae5
JG
3566int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock,
3567 struct notification_thread_handle *notification_thread)
3568{
3569 int ret;
3570 size_t trigger_len;
3571 ssize_t sock_recv_len;
3572 struct lttng_trigger *trigger = NULL;
3573 struct lttng_buffer_view view;
3574 struct lttng_dynamic_buffer trigger_buffer;
3575
3576 lttng_dynamic_buffer_init(&trigger_buffer);
3577 trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length;
3578 ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len);
3579 if (ret) {
3580 ret = LTTNG_ERR_NOMEM;
3581 goto end;
3582 }
3583
3584 sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data,
3585 trigger_len);
3586 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
3587 ERR("Failed to receive \"register trigger\" command payload");
3588 /* TODO: should this be a new error enum ? */
3589 ret = LTTNG_ERR_INVALID_TRIGGER;
3590 goto end;
3591 }
3592
3593 view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1);
3594 if (lttng_trigger_create_from_buffer(&view, &trigger) !=
3595 trigger_len) {
3596 ERR("Invalid trigger payload received in \"register trigger\" command");
3597 ret = LTTNG_ERR_INVALID_TRIGGER;
3598 goto end;
3599 }
3600
3601 ret = notification_thread_command_register_trigger(notification_thread,
3602 trigger);
587f9fd0
JG
3603 /* Ownership of trigger was transferred. */
3604 trigger = NULL;
b0880ae5 3605end:
587f9fd0 3606 lttng_trigger_destroy(trigger);
b0880ae5
JG
3607 lttng_dynamic_buffer_reset(&trigger_buffer);
3608 return ret;
3609}
3610
3611int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock,
3612 struct notification_thread_handle *notification_thread)
3613{
3614 int ret;
3615 size_t trigger_len;
3616 ssize_t sock_recv_len;
3617 struct lttng_trigger *trigger = NULL;
3618 struct lttng_buffer_view view;
3619 struct lttng_dynamic_buffer trigger_buffer;
3620
3621 lttng_dynamic_buffer_init(&trigger_buffer);
3622 trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length;
3623 ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len);
3624 if (ret) {
3625 ret = LTTNG_ERR_NOMEM;
3626 goto end;
3627 }
3628
3629 sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data,
3630 trigger_len);
3631 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
3632 ERR("Failed to receive \"unregister trigger\" command payload");
3633 /* TODO: should this be a new error enum ? */
3634 ret = LTTNG_ERR_INVALID_TRIGGER;
3635 goto end;
3636 }
3637
3638 view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1);
3639 if (lttng_trigger_create_from_buffer(&view, &trigger) !=
3640 trigger_len) {
3641 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3642 ret = LTTNG_ERR_INVALID_TRIGGER;
3643 goto end;
3644 }
3645
3646 ret = notification_thread_command_unregister_trigger(notification_thread,
3647 trigger);
3648end:
587f9fd0 3649 lttng_trigger_destroy(trigger);
b0880ae5
JG
3650 lttng_dynamic_buffer_reset(&trigger_buffer);
3651 return ret;
3652}
3653
6dc3064a
DG
3654/*
3655 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3656 * snapshot output is *not* set with a remote destination.
3657 *
a934f4af 3658 * Return 0 on success or a LTTNG_ERR code.
6dc3064a
DG
3659 */
3660static int set_relayd_for_snapshot(struct consumer_output *consumer,
3661 struct snapshot_output *snap_output, struct ltt_session *session)
3662{
a934f4af 3663 int ret = LTTNG_OK;
6dc3064a
DG
3664 struct lttng_ht_iter iter;
3665 struct consumer_socket *socket;
3666
3667 assert(consumer);
3668 assert(snap_output);
3669 assert(session);
3670
3671 DBG2("Set relayd object from snapshot output");
3672
3673 /* Ignore if snapshot consumer output is not network. */
3674 if (snap_output->consumer->type != CONSUMER_DST_NET) {
3675 goto error;
3676 }
3677
3678 /*
3679 * For each consumer socket, create and send the relayd object of the
3680 * snapshot output.
3681 */
3682 rcu_read_lock();
5eecee74
DG
3683 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
3684 socket, node.node) {
6dc3064a 3685 ret = send_consumer_relayd_sockets(0, session->id,
d3e2ba59
JD
3686 snap_output->consumer, socket,
3687 session->name, session->hostname,
3688 session->live_timer);
a934f4af 3689 if (ret != LTTNG_OK) {
6dc3064a
DG
3690 rcu_read_unlock();
3691 goto error;
3692 }
3693 }
3694 rcu_read_unlock();
3695
3696error:
3697 return ret;
3698}
3699
3700/*
3701 * Record a kernel snapshot.
3702 *
fac41e72 3703 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a
DG
3704 */
3705static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
5c786ded 3706 struct snapshot_output *output, struct ltt_session *session,
d07ceecd 3707 int wait, uint64_t nb_packets_per_stream)
6dc3064a
DG
3708{
3709 int ret;
3710
3711 assert(ksess);
3712 assert(output);
3713 assert(session);
3714
10a50311 3715
af706bb7
DG
3716 /*
3717 * Copy kernel session sockets so we can communicate with the right
3718 * consumer for the snapshot record command.
3719 */
3720 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
3721 if (ret < 0) {
a934f4af 3722 ret = LTTNG_ERR_NOMEM;
af706bb7 3723 goto error;
6dc3064a
DG
3724 }
3725
3726 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
a934f4af 3727 if (ret != LTTNG_OK) {
af706bb7 3728 goto error_snapshot;
6dc3064a
DG
3729 }
3730
d07ceecd 3731 ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
2a06df8d 3732 if (ret != LTTNG_OK) {
af706bb7 3733 goto error_snapshot;
6dc3064a
DG
3734 }
3735
a934f4af 3736 ret = LTTNG_OK;
1371fc12 3737 goto end;
a934f4af 3738
af706bb7
DG
3739error_snapshot:
3740 /* Clean up copied sockets so this output can use some other later on. */
3741 consumer_destroy_output_sockets(output->consumer);
6dc3064a 3742error:
1371fc12 3743end:
6dc3064a
DG
3744 return ret;
3745}
3746
3747/*
3748 * Record a UST snapshot.
3749 *
a934f4af 3750 * Return 0 on success or a LTTNG_ERR error code.
6dc3064a
DG
3751 */
3752static int record_ust_snapshot(struct ltt_ust_session *usess,
5c786ded 3753 struct snapshot_output *output, struct ltt_session *session,
d07ceecd 3754 int wait, uint64_t nb_packets_per_stream)
6dc3064a
DG
3755{
3756 int ret;
3757
3758 assert(usess);
3759 assert(output);
3760 assert(session);
3761
af706bb7 3762 /*
d3f14b8a 3763 * Copy UST session sockets so we can communicate with the right
af706bb7
DG
3764 * consumer for the snapshot record command.
3765 */
3766 ret = consumer_copy_sockets(output->consumer, usess->consumer);
3767 if (ret < 0) {
a934f4af 3768 ret = LTTNG_ERR_NOMEM;
af706bb7 3769 goto error;
6dc3064a
DG
3770 }
3771
3772 ret = set_relayd_for_snapshot(usess->consumer, output, session);
a934f4af 3773 if (ret != LTTNG_OK) {
af706bb7 3774 goto error_snapshot;
6dc3064a
DG
3775 }
3776
d07ceecd 3777 ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
6dc3064a 3778 if (ret < 0) {
7badf927
DG
3779 switch (-ret) {
3780 case EINVAL:
a934f4af 3781 ret = LTTNG_ERR_INVALID;
7badf927 3782 break;
7badf927
DG
3783 default:
3784 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3785 break;
5c786ded 3786 }
af706bb7 3787 goto error_snapshot;
6dc3064a
DG
3788 }
3789
a934f4af
DG
3790 ret = LTTNG_OK;
3791
af706bb7
DG
3792error_snapshot:
3793 /* Clean up copied sockets so this output can use some other later on. */
3794 consumer_destroy_output_sockets(output->consumer);
6dc3064a
DG
3795error:
3796 return ret;
3797}
3798
d07ceecd
MD
3799static
3800uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
3801 uint64_t cur_nr_packets)
68808f4e 3802{
d07ceecd 3803 uint64_t tot_size = 0;
68808f4e
DG
3804
3805 if (session->kernel_session) {
3806 struct ltt_kernel_channel *chan;
3807 struct ltt_kernel_session *ksess = session->kernel_session;
3808
68808f4e 3809 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
d07ceecd
MD
3810 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
3811 /*
3812 * Don't take channel into account if we
3813 * already grab all its packets.
3814 */
3815 continue;
68808f4e 3816 }
d07ceecd
MD
3817 tot_size += chan->channel->attr.subbuf_size
3818 * chan->stream_count;
68808f4e
DG
3819 }
3820 }
3821
3822 if (session->ust_session) {
68808f4e
DG
3823 struct ltt_ust_session *usess = session->ust_session;
3824
d07ceecd
MD
3825 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
3826 cur_nr_packets);
68808f4e
DG
3827 }
3828
d07ceecd 3829 return tot_size;
68808f4e
DG
3830}
3831
5c786ded 3832/*
d07ceecd
MD
3833 * Calculate the number of packets we can grab from each stream that
3834 * fits within the overall snapshot max size.
3835 *
3836 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3837 * the number of packets per stream.
3838 *
3839 * TODO: this approach is not perfect: we consider the worse case
3840 * (packet filling the sub-buffers) as an upper bound, but we could do
3841 * better if we do this calculation while we actually grab the packet
3842 * content: we would know how much padding we don't actually store into
3843 * the file.
3844 *
3845 * This algorithm is currently bounded by the number of packets per
3846 * stream.
3847 *
3848 * Since we call this algorithm before actually grabbing the data, it's
3849 * an approximation: for instance, applications could appear/disappear
3850 * in between this call and actually grabbing data.
5c786ded 3851 */
d07ceecd
MD
3852static
3853int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
5c786ded 3854{
d07ceecd
MD
3855 int64_t size_left;
3856 uint64_t cur_nb_packets = 0;
5c786ded 3857
d07ceecd
MD
3858 if (!max_size) {
3859 return 0; /* Infinite */
5c786ded
JD
3860 }
3861
d07ceecd
MD
3862 size_left = max_size;
3863 for (;;) {
3864 uint64_t one_more_packet_tot_size;
5c786ded 3865
d07ceecd
MD
3866 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
3867 cur_nb_packets);
3868 if (!one_more_packet_tot_size) {
3869 /* We are already grabbing all packets. */
3870 break;
3871 }
3872 size_left -= one_more_packet_tot_size;
3873 if (size_left < 0) {
3874 break;
3875 }
3876 cur_nb_packets++;
5c786ded 3877 }
d07ceecd
MD
3878 if (!cur_nb_packets) {
3879 /* Not enough room to grab one packet of each stream, error. */
3880 return -1;
3881 }
3882 return cur_nb_packets;
5c786ded
JD
3883}
3884
6dc3064a
DG
3885/*
3886 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3887 *
3888 * The wait parameter is ignored so this call always wait for the snapshot to
3889 * complete before returning.
3890 *
3891 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3892 */
3893int cmd_snapshot_record(struct ltt_session *session,
3894 struct lttng_snapshot_output *output, int wait)
3895{
3896 int ret = LTTNG_OK;
e1986656
DG
3897 unsigned int use_tmp_output = 0;
3898 struct snapshot_output tmp_output;
00e1dfc4 3899 unsigned int snapshot_success = 0;
10ba83fe 3900 char datetime[16];
6dc3064a
DG
3901
3902 assert(session);
ba45d9f0 3903 assert(output);
6dc3064a
DG
3904
3905 DBG("Cmd snapshot record for session %s", session->name);
3906
10ba83fe
JR
3907 /* Get the datetime for the snapshot output directory. */
3908 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
3909 sizeof(datetime));
3910 if (!ret) {
3911 ret = LTTNG_ERR_INVALID;
3912 goto error;
3913 }
3914
6dc3064a 3915 /*
d3f14b8a
MD
3916 * Permission denied to create an output if the session is not
3917 * set in no output mode.
6dc3064a
DG
3918 */
3919 if (session->output_traces) {
903ef685 3920 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3921 goto error;
3922 }
3923
3924 /* The session needs to be started at least once. */
8382cf6f 3925 if (!session->has_been_started) {
6dc3064a
DG
3926 ret = LTTNG_ERR_START_SESSION_ONCE;
3927 goto error;
3928 }
3929
3930 /* Use temporary output for the session. */
ba45d9f0 3931 if (*output->ctrl_url != '\0') {
6dc3064a
DG
3932 ret = snapshot_output_init(output->max_size, output->name,
3933 output->ctrl_url, output->data_url, session->consumer,
e1986656 3934 &tmp_output, NULL);
6dc3064a
DG
3935 if (ret < 0) {
3936 if (ret == -ENOMEM) {
3937 ret = LTTNG_ERR_NOMEM;
3938 } else {
3939 ret = LTTNG_ERR_INVALID;
3940 }
3941 goto error;
3942 }
1bfe7328
DG
3943 /* Use the global session count for the temporary snapshot. */
3944 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
10ba83fe
JR
3945
3946 /* Use the global datetime */
3947 memcpy(tmp_output.datetime, datetime, sizeof(datetime));
e1986656 3948 use_tmp_output = 1;
6dc3064a
DG
3949 }
3950
804c90a8
JR
3951 if (use_tmp_output) {
3952 int64_t nb_packets_per_stream;
6dc3064a 3953
804c90a8
JR
3954 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3955 tmp_output.max_size);
3956 if (nb_packets_per_stream < 0) {
3957 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3958 goto error;
3959 }
d07ceecd 3960
804c90a8
JR
3961 if (session->kernel_session) {
3962 ret = record_kernel_snapshot(session->kernel_session,
3963 &tmp_output, session,
3964 wait, nb_packets_per_stream);
3965 if (ret != LTTNG_OK) {
d07ceecd
MD
3966 goto error;
3967 }
804c90a8
JR
3968 }
3969
3970 if (session->ust_session) {
3971 ret = record_ust_snapshot(session->ust_session,
3972 &tmp_output, session,
d07ceecd 3973 wait, nb_packets_per_stream);
a934f4af 3974 if (ret != LTTNG_OK) {
6dc3064a
DG
3975 goto error;
3976 }
804c90a8 3977 }
e1986656 3978
804c90a8
JR
3979 snapshot_success = 1;
3980 } else {
3981 struct snapshot_output *sout;
3982 struct lttng_ht_iter iter;
68808f4e 3983
804c90a8
JR
3984 rcu_read_lock();
3985 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3986 &iter.iter, sout, node.node) {
3987 int64_t nb_packets_per_stream;
e1986656 3988
804c90a8
JR
3989 /*
3990 * Make a local copy of the output and assign the possible
3991 * temporary value given by the caller.
3992 */
3993 memset(&tmp_output, 0, sizeof(tmp_output));
3994 memcpy(&tmp_output, sout, sizeof(tmp_output));
1bfe7328 3995
804c90a8
JR
3996 if (output->max_size != (uint64_t) -1ULL) {
3997 tmp_output.max_size = output->max_size;
6dc3064a 3998 }
d07ceecd
MD
3999
4000 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
4001 tmp_output.max_size);
4002 if (nb_packets_per_stream < 0) {
4003 ret = LTTNG_ERR_MAX_SIZE_INVALID;
804c90a8 4004 rcu_read_unlock();
d07ceecd
MD
4005 goto error;
4006 }
6dc3064a 4007
804c90a8
JR
4008 /* Use temporary name. */
4009 if (*output->name != '\0') {
cf3e357d
MD
4010 if (lttng_strncpy(tmp_output.name, output->name,
4011 sizeof(tmp_output.name))) {
4012 ret = LTTNG_ERR_INVALID;
4013 rcu_read_unlock();
4014 goto error;
4015 }
804c90a8 4016 }
e1986656 4017
804c90a8 4018 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
10ba83fe 4019 memcpy(tmp_output.datetime, datetime, sizeof(datetime));
e1986656 4020
804c90a8
JR
4021 if (session->kernel_session) {
4022 ret = record_kernel_snapshot(session->kernel_session,
4023 &tmp_output, session,
4024 wait, nb_packets_per_stream);
4025 if (ret != LTTNG_OK) {
d07ceecd 4026 rcu_read_unlock();
68808f4e
DG
4027 goto error;
4028 }
804c90a8 4029 }
68808f4e 4030
804c90a8
JR
4031 if (session->ust_session) {
4032 ret = record_ust_snapshot(session->ust_session,
4033 &tmp_output, session,
d07ceecd 4034 wait, nb_packets_per_stream);
a934f4af 4035 if (ret != LTTNG_OK) {
6dc3064a
DG
4036 rcu_read_unlock();
4037 goto error;
4038 }
4039 }
804c90a8 4040 snapshot_success = 1;
6dc3064a 4041 }
804c90a8 4042 rcu_read_unlock();
6dc3064a
DG
4043 }
4044
1bfe7328
DG
4045 if (snapshot_success) {
4046 session->snapshot.nb_snapshot++;
b67578cb
MD
4047 } else {
4048 ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
4049 }
4050
6dc3064a 4051error:
6dc3064a
DG
4052 return ret;
4053}
4054
d7ba1388
MD
4055/*
4056 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4057 */
4058int cmd_set_session_shm_path(struct ltt_session *session,
4059 const char *shm_path)
4060{
4061 /* Safety net */
4062 assert(session);
4063
4064 /*
4065 * Can only set shm path before session is started.
4066 */
4067 if (session->has_been_started) {
4068 return LTTNG_ERR_SESSION_STARTED;
4069 }
4070
4071 strncpy(session->shm_path, shm_path,
4072 sizeof(session->shm_path));
4073 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
4074
4075 return 0;
4076}
4077
2f77fc4b
DG
4078/*
4079 * Init command subsystem.
4080 */
4081void cmd_init(void)
4082{
4083 /*
d88aee68
DG
4084 * Set network sequence index to 1 for streams to match a relayd
4085 * socket on the consumer side.
2f77fc4b 4086 */
d88aee68
DG
4087 pthread_mutex_lock(&relayd_net_seq_idx_lock);
4088 relayd_net_seq_idx = 1;
4089 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
4090
4091 DBG("Command subsystem initialized");
4092}
This page took 0.266254 seconds and 4 git commands to generate.