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