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