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