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