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