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