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