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