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