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