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