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