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