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