2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
3 * Copyright 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_COMP_LOG_SELF_COMP (viewer_connection->self_comp)
25 #define BT_LOG_OUTPUT_LEVEL (viewer_connection->log_level)
26 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/VIEWER"
27 #include "logging/comp-logging.h"
34 #include <sys/types.h>
39 #include "compat/socket.h"
40 #include "compat/endian.h"
41 #include "compat/compiler.h"
42 #include "common/common.h"
43 #include <babeltrace2/babeltrace.h>
45 #include "lttng-live.h"
46 #include "viewer-connection.h"
47 #include "lttng-viewer-abi.h"
48 #include "data-stream.h"
51 #define viewer_handle_send_recv_status(_self_comp, _self_comp_class, \
52 _status, _action, _msg_str) \
55 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: \
57 case LTTNG_LIVE_VIEWER_STATUS_ERROR: \
58 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, \
59 _self_comp_class, "Error " _action " " _msg_str); \
66 #define viewer_handle_send_status(_self_comp, _self_comp_class, _status, _msg_str) \
67 viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, \
70 #define viewer_handle_recv_status(_self_comp, _self_comp_class, _status, _msg_str) \
71 viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, \
72 "receiving", _msg_str)
74 #define LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp, \
75 _self_comp_class, _msg, _fmt, ...) \
77 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, \
78 _msg ": %s" _fmt, bt_socket_errormsg(), ##__VA_ARGS__); \
82 enum lttng_live_iterator_status
viewer_status_to_live_iterator_status(
83 enum lttng_live_viewer_status viewer_status
)
85 switch (viewer_status
) {
86 case LTTNG_LIVE_VIEWER_STATUS_OK
:
87 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
88 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
:
89 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
90 case LTTNG_LIVE_VIEWER_STATUS_ERROR
:
91 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
98 enum ctf_msg_iter_medium_status
viewer_status_to_ctf_msg_iter_medium_status(
99 enum lttng_live_viewer_status viewer_status
)
101 switch (viewer_status
) {
102 case LTTNG_LIVE_VIEWER_STATUS_OK
:
103 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
104 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
:
105 return CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
;
106 case LTTNG_LIVE_VIEWER_STATUS_ERROR
:
107 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
114 * This function receives a message from the Relay daemon.
115 * If it received the entire message, it returns _OK,
116 * If it's interrupted, it returns _INTERRUPTED,
117 * otherwise, it returns _ERROR.
120 enum lttng_live_viewer_status
lttng_live_recv(
121 struct live_viewer_connection
*viewer_connection
,
122 void *buf
, size_t len
)
125 bt_self_component_class
*self_comp_class
=
126 viewer_connection
->self_comp_class
;
127 bt_self_component
*self_comp
=
128 viewer_connection
->self_comp
;
129 size_t total_received
= 0, to_receive
= len
;
130 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
131 viewer_connection
->lttng_live_msg_iter
;
132 enum lttng_live_viewer_status status
;
133 BT_SOCKET sock
= viewer_connection
->control_sock
;
136 * Receive a message from the Relay.
139 received
= bt_socket_recv(sock
, buf
+ total_received
, to_receive
, 0);
140 if (received
== BT_SOCKET_ERROR
) {
141 if (bt_socket_interrupted()) {
142 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
144 * This interruption was due to a
145 * SIGINT and the graph is being torn
148 status
= LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
;
149 lttng_live_msg_iter
->was_interrupted
= true;
153 * A signal was received, but the graph
154 * is not being torn down. Carry on.
160 * For any other types of socket error, returng
163 LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(
164 self_comp
, self_comp_class
,
165 "Error receiving from Relay", ".");
166 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
169 } else if (received
== 0) {
171 * The recv() call returned 0. This means the
172 * connection was orderly shutdown from the other peer.
173 * If that happens when we are trying to receive
174 * a message from it, it means something when wrong.
176 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
177 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
178 self_comp_class
, "Remote side has closed connection");
182 BT_ASSERT(received
<= to_receive
);
183 total_received
+= received
;
184 to_receive
-= received
;
186 } while (to_receive
> 0);
188 BT_ASSERT(total_received
== len
);
189 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
196 * This function sends a message to the Relay daemon.
197 * If it send the message, it returns _OK,
198 * If it's interrupted, it returns _INTERRUPTED,
199 * otherwise, it returns _ERROR.
202 enum lttng_live_viewer_status
lttng_live_send(
203 struct live_viewer_connection
*viewer_connection
,
204 const void *buf
, size_t len
)
206 enum lttng_live_viewer_status status
;
207 bt_self_component_class
*self_comp_class
=
208 viewer_connection
->self_comp_class
;
209 bt_self_component
*self_comp
=
210 viewer_connection
->self_comp
;
211 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
212 viewer_connection
->lttng_live_msg_iter
;
213 BT_SOCKET sock
= viewer_connection
->control_sock
;
214 size_t to_send
= len
;
215 ssize_t total_sent
= 0;
218 ssize_t sent
= bt_socket_send_nosigpipe(sock
, buf
+ total_sent
,
220 if (sent
== BT_SOCKET_ERROR
) {
221 if (bt_socket_interrupted()) {
222 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
224 * This interruption was a SIGINT and
225 * the graph is being teared down.
227 status
= LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
;
228 lttng_live_msg_iter
->was_interrupted
= true;
232 * A signal was received, but the graph
233 * is not being teared down. Carry on.
239 * The send() call returned an error.
241 LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(
242 self_comp
, self_comp_class
,
243 "Error sending to Relay", ".");
244 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
249 BT_ASSERT(sent
<= to_send
);
253 } while (to_send
> 0);
255 BT_ASSERT(total_sent
== len
);
256 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
263 int parse_url(struct live_viewer_connection
*viewer_connection
)
265 char error_buf
[256] = { 0 };
266 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
267 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
268 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
270 const char *path
= viewer_connection
->url
->str
;
276 lttng_live_url_parts
= bt_common_parse_lttng_live_url(path
, error_buf
,
278 if (!lttng_live_url_parts
.proto
) {
279 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
280 self_comp_class
,"Invalid LTTng live URL format: %s",
284 viewer_connection
->proto
= lttng_live_url_parts
.proto
;
285 lttng_live_url_parts
.proto
= NULL
;
287 viewer_connection
->relay_hostname
= lttng_live_url_parts
.hostname
;
288 lttng_live_url_parts
.hostname
= NULL
;
290 if (lttng_live_url_parts
.port
>= 0) {
291 viewer_connection
->port
= lttng_live_url_parts
.port
;
293 viewer_connection
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
296 viewer_connection
->target_hostname
= lttng_live_url_parts
.target_hostname
;
297 lttng_live_url_parts
.target_hostname
= NULL
;
299 if (lttng_live_url_parts
.session_name
) {
300 viewer_connection
->session_name
= lttng_live_url_parts
.session_name
;
301 lttng_live_url_parts
.session_name
= NULL
;
307 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
312 enum lttng_live_viewer_status
lttng_live_handshake(
313 struct live_viewer_connection
*viewer_connection
)
315 struct lttng_viewer_cmd cmd
;
316 struct lttng_viewer_connect connect
;
317 enum lttng_live_viewer_status status
;
318 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
319 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
320 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(connect
);
321 char cmd_buf
[cmd_buf_len
];
323 BT_COMP_OR_COMP_CLASS_LOGD(self_comp
, self_comp_class
,
324 "Handshaking with the Relay: "
325 "major-version=%u, minor-version=%u",
326 LTTNG_LIVE_MAJOR
, LTTNG_LIVE_MINOR
);
328 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
329 cmd
.data_size
= htobe64((uint64_t) sizeof(connect
));
330 cmd
.cmd_version
= htobe32(0);
332 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
333 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
334 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
335 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
338 * Merge the cmd and connection request to prevent a write-write
339 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
340 * second write to be performed quickly in presence of Nagle's algorithm
342 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
343 memcpy(cmd_buf
+ sizeof(cmd
), &connect
, sizeof(connect
));
345 status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
346 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
347 viewer_handle_send_status(self_comp
, self_comp_class
,
348 status
, "viewer connect command");
352 status
= lttng_live_recv(viewer_connection
, &connect
, sizeof(connect
));
353 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
354 viewer_handle_recv_status(self_comp
, self_comp_class
,
355 status
, "viewer connect reply");
359 BT_COMP_OR_COMP_CLASS_LOGI(self_comp
, self_comp_class
,
360 "Received viewer session ID : %" PRIu64
,
361 (uint64_t) be64toh(connect
.viewer_session_id
));
362 BT_COMP_OR_COMP_CLASS_LOGI(self_comp
, self_comp_class
,
363 "Relayd version : %u.%u", be32toh(connect
.major
),
364 be32toh(connect
.minor
));
366 if (LTTNG_LIVE_MAJOR
!= be32toh(connect
.major
)) {
367 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
368 self_comp_class
, "Incompatible lttng-relayd protocol");
369 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
372 /* Use the smallest protocol version implemented. */
373 if (LTTNG_LIVE_MINOR
> be32toh(connect
.minor
)) {
374 viewer_connection
->minor
= be32toh(connect
.minor
);
376 viewer_connection
->minor
= LTTNG_LIVE_MINOR
;
378 viewer_connection
->major
= LTTNG_LIVE_MAJOR
;
380 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
389 enum lttng_live_viewer_status
lttng_live_connect_viewer(
390 struct live_viewer_connection
*viewer_connection
)
392 struct hostent
*host
;
393 struct sockaddr_in server_addr
;
394 enum lttng_live_viewer_status status
;
395 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
396 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
398 if (parse_url(viewer_connection
)) {
399 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
400 self_comp_class
, "Failed to parse URL");
401 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
405 BT_COMP_OR_COMP_CLASS_LOGD(self_comp
, self_comp_class
,
406 "Connecting to hostname : %s, port : %d, "
407 "target hostname : %s, session name : %s, proto : %s",
408 viewer_connection
->relay_hostname
->str
,
409 viewer_connection
->port
,
410 !viewer_connection
->target_hostname
?
411 "<none>" : viewer_connection
->target_hostname
->str
,
412 !viewer_connection
->session_name
?
413 "<none>" : viewer_connection
->session_name
->str
,
414 viewer_connection
->proto
->str
);
416 host
= gethostbyname(viewer_connection
->relay_hostname
->str
);
418 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
419 self_comp_class
, "Cannot lookup hostname: hostname=\"%s\"",
420 viewer_connection
->relay_hostname
->str
);
421 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
425 if ((viewer_connection
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == BT_INVALID_SOCKET
) {
426 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
427 self_comp_class
, "Socket creation failed: %s", bt_socket_errormsg());
428 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
432 server_addr
.sin_family
= AF_INET
;
433 server_addr
.sin_port
= htons(viewer_connection
->port
);
434 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
435 memset(&(server_addr
.sin_zero
), 0, 8);
437 if (connect(viewer_connection
->control_sock
, (struct sockaddr
*) &server_addr
,
438 sizeof(struct sockaddr
)) == BT_SOCKET_ERROR
) {
439 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
440 self_comp_class
, "Connection failed: %s",
441 bt_socket_errormsg());
442 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
446 status
= lttng_live_handshake(viewer_connection
);
449 * Only print error and append cause in case of error. not in case of
452 if (status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
453 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
454 self_comp_class
, "Viewer handshake failed");
456 } else if (status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
463 if (viewer_connection
->control_sock
!= BT_INVALID_SOCKET
) {
464 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
465 BT_COMP_OR_COMP_CLASS_LOGW(self_comp
, self_comp_class
,
466 "Error closing socket: %s.", bt_socket_errormsg());
469 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
475 void lttng_live_disconnect_viewer(
476 struct live_viewer_connection
*viewer_connection
)
478 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
479 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
481 if (viewer_connection
->control_sock
== BT_INVALID_SOCKET
) {
484 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
485 BT_COMP_OR_COMP_CLASS_LOGW(self_comp
, self_comp_class
,
486 "Error closing socket: %s", bt_socket_errormsg());
487 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
492 int list_update_session(bt_value
*results
,
493 const struct lttng_viewer_session
*session
,
494 bool *_found
, struct live_viewer_connection
*viewer_connection
)
496 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
497 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
500 bt_value
*map
= NULL
;
501 bt_value
*hostname
= NULL
;
502 bt_value
*session_name
= NULL
;
503 bt_value
*btval
= NULL
;
506 len
= bt_value_array_get_length(results
);
507 for (i
= 0; i
< len
; i
++) {
508 const char *hostname_str
= NULL
;
509 const char *session_name_str
= NULL
;
511 map
= bt_value_array_borrow_element_by_index(results
, i
);
512 hostname
= bt_value_map_borrow_entry_value(map
, "target-hostname");
514 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
516 "Error borrowing \"target-hostname\" entry.");
520 session_name
= bt_value_map_borrow_entry_value(map
, "session-name");
522 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
524 "Error borrowing \"session-name\" entry.");
528 hostname_str
= bt_value_string_get(hostname
);
529 session_name_str
= bt_value_string_get(session_name
);
531 if (strcmp(session
->hostname
, hostname_str
) == 0
532 && strcmp(session
->session_name
, session_name_str
) == 0) {
534 uint32_t streams
= be32toh(session
->streams
);
535 uint32_t clients
= be32toh(session
->clients
);
539 btval
= bt_value_map_borrow_entry_value(map
, "stream-count");
541 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
542 self_comp
, self_comp_class
,
543 "Error borrowing \"stream-count\" entry.");
547 val
= bt_value_integer_unsigned_get(btval
);
550 bt_value_integer_unsigned_set(btval
, val
);
552 btval
= bt_value_map_borrow_entry_value(map
, "client-count");
554 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
555 self_comp
, self_comp_class
,
556 "Error borrowing \"client-count\" entry.");
560 val
= bt_value_integer_unsigned_get(btval
);
562 val
= bt_max_t(int64_t, clients
, val
);
563 bt_value_integer_unsigned_set(btval
, val
);
576 int list_append_session(bt_value
*results
,
578 const struct lttng_viewer_session
*session
,
579 struct live_viewer_connection
*viewer_connection
)
582 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
583 bt_value_map_insert_entry_status insert_status
;
584 bt_value_array_append_element_status append_status
;
585 bt_value
*map
= NULL
;
590 * If the session already exists, add the stream count to it,
591 * and do max of client counts.
593 ret
= list_update_session(results
, session
, &found
, viewer_connection
);
598 map
= bt_value_map_create();
600 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
601 "Error creating map value.");
606 if (base_url
->len
< 1) {
607 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
608 "Error: base_url length smaller than 1.");
616 url
= g_string_new(base_url
->str
);
617 g_string_append(url
, "/host/");
618 g_string_append(url
, session
->hostname
);
619 g_string_append_c(url
, '/');
620 g_string_append(url
, session
->session_name
);
622 insert_status
= bt_value_map_insert_string_entry(map
, "url", url
->str
);
623 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
624 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
625 "Error inserting \"url\" entry.");
631 * key = "target-hostname",
634 insert_status
= bt_value_map_insert_string_entry(map
, "target-hostname",
636 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
637 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
638 "Error inserting \"target-hostname\" entry.");
644 * key = "session-name",
647 insert_status
= bt_value_map_insert_string_entry(map
, "session-name",
648 session
->session_name
);
649 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
650 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
651 "Error inserting \"session-name\" entry.");
661 uint32_t live_timer
= be32toh(session
->live_timer
);
663 insert_status
= bt_value_map_insert_unsigned_integer_entry(
664 map
, "timer-us", live_timer
);
665 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
666 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
667 "Error inserting \"timer-us\" entry.");
674 * key = "stream-count",
678 uint32_t streams
= be32toh(session
->streams
);
680 insert_status
= bt_value_map_insert_unsigned_integer_entry(map
,
681 "stream-count", streams
);
682 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
683 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
684 "Error inserting \"stream-count\" entry.");
691 * key = "client-count",
695 uint32_t clients
= be32toh(session
->clients
);
697 insert_status
= bt_value_map_insert_unsigned_integer_entry(map
,
698 "client-count", clients
);
699 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
700 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
701 "Error inserting \"client-count\" entry.");
707 append_status
= bt_value_array_append_element(results
, map
);
708 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
709 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
710 "Error appending map to results.");
716 g_string_free(url
, true);
718 BT_VALUE_PUT_REF_AND_RESET(map
);
723 * Data structure returned:
734 * key = "target-hostname",
738 * key = "session-name",
746 * key = "stream-count",
750 * key = "client-count",
759 bt_component_class_query_method_status
live_viewer_connection_list_sessions(
760 struct live_viewer_connection
*viewer_connection
,
761 const bt_value
**user_result
)
763 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
764 bt_component_class_query_method_status status
=
765 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
766 bt_value
*result
= NULL
;
767 enum lttng_live_viewer_status viewer_status
;
768 struct lttng_viewer_cmd cmd
;
769 struct lttng_viewer_list_sessions list
;
770 uint32_t i
, sessions_count
;
772 result
= bt_value_array_create();
774 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
775 "Error creating array");
776 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
780 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
781 cmd
.data_size
= htobe64((uint64_t) 0);
782 cmd
.cmd_version
= htobe32(0);
784 viewer_status
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
785 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
786 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
787 "Error sending list sessions command");
788 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
790 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
791 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
795 viewer_status
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
796 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
797 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
798 "Error receiving session list");
799 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
801 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
802 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
806 sessions_count
= be32toh(list
.sessions_count
);
807 for (i
= 0; i
< sessions_count
; i
++) {
808 struct lttng_viewer_session lsession
;
810 viewer_status
= lttng_live_recv(viewer_connection
, &lsession
,
812 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
813 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
814 "Error receiving session:");
815 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
817 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
818 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
822 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
823 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
824 if (list_append_session(result
, viewer_connection
->url
,
825 &lsession
, viewer_connection
)) {
826 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
827 "Error appending session");
828 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
833 *user_result
= result
;
836 BT_VALUE_PUT_REF_AND_RESET(result
);
842 enum lttng_live_viewer_status
lttng_live_query_session_ids(
843 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
845 struct lttng_viewer_cmd cmd
;
846 struct lttng_viewer_list_sessions list
;
847 struct lttng_viewer_session lsession
;
848 uint32_t i
, sessions_count
;
850 enum lttng_live_viewer_status status
;
851 struct live_viewer_connection
*viewer_connection
=
852 lttng_live_msg_iter
->viewer_connection
;
853 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
854 bt_self_component_class
*self_comp_class
=
855 viewer_connection
->self_comp_class
;
857 BT_COMP_LOGD("Asking the Relay for the list of sessions");
859 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
860 cmd
.data_size
= htobe64((uint64_t) 0);
861 cmd
.cmd_version
= htobe32(0);
863 status
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
864 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
865 viewer_handle_send_status(self_comp
, self_comp_class
,
866 status
, "list sessions command");
870 status
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
871 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
872 viewer_handle_recv_status(self_comp
, self_comp_class
,
873 status
, "session list reply");
877 sessions_count
= be32toh(list
.sessions_count
);
878 for (i
= 0; i
< sessions_count
; i
++) {
879 status
= lttng_live_recv(viewer_connection
, &lsession
,
881 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
882 viewer_handle_recv_status(self_comp
, self_comp_class
,
883 status
, "session reply");
886 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
887 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
888 session_id
= be64toh(lsession
.id
);
890 BT_COMP_LOGI("Adding session to internal list: "
891 "session-id=%" PRIu64
", hostname=\"%s\", session-name=\"%s\"",
892 session_id
, lsession
.hostname
, lsession
.session_name
);
894 if ((strncmp(lsession
.session_name
,
895 viewer_connection
->session_name
->str
,
896 LTTNG_VIEWER_NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
897 viewer_connection
->target_hostname
->str
,
898 LTTNG_VIEWER_HOST_NAME_MAX
) == 0)) {
900 if (lttng_live_add_session(lttng_live_msg_iter
, session_id
,
902 lsession
.session_name
)) {
903 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
904 "Failed to add live session");
905 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
911 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
918 enum lttng_live_viewer_status
lttng_live_create_viewer_session(
919 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
921 struct lttng_viewer_cmd cmd
;
922 struct lttng_viewer_create_session_response resp
;
923 enum lttng_live_viewer_status status
;
924 struct live_viewer_connection
*viewer_connection
=
925 lttng_live_msg_iter
->viewer_connection
;
926 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
927 bt_self_component_class
*self_comp_class
=
928 viewer_connection
->self_comp_class
;
930 BT_COMP_OR_COMP_CLASS_LOGD(self_comp
, self_comp_class
,
931 "Creating a viewer session");
933 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
934 cmd
.data_size
= htobe64((uint64_t) 0);
935 cmd
.cmd_version
= htobe32(0);
937 status
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
938 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
939 viewer_handle_send_status(self_comp
, self_comp_class
,
940 status
, "create session command");
944 status
= lttng_live_recv(viewer_connection
, &resp
, sizeof(resp
));
945 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
946 viewer_handle_recv_status(self_comp
, self_comp_class
,
947 status
, "create session reply");
951 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
952 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
953 "Error creating viewer session");
954 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
958 status
= lttng_live_query_session_ids(lttng_live_msg_iter
);
959 if (status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
960 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
961 "Failed to query live viewer session ids");
963 } else if (status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
972 enum lttng_live_viewer_status
receive_streams(struct lttng_live_session
*session
,
973 uint32_t stream_count
,
974 bt_self_message_iterator
*self_msg_iter
)
977 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
978 session
->lttng_live_msg_iter
;
979 enum lttng_live_viewer_status status
;
980 struct live_viewer_connection
*viewer_connection
=
981 lttng_live_msg_iter
->viewer_connection
;
982 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
984 BT_COMP_LOGI("Getting %" PRIu32
" new streams:", stream_count
);
985 for (i
= 0; i
< stream_count
; i
++) {
986 struct lttng_viewer_stream stream
;
987 struct lttng_live_stream_iterator
*live_stream
;
989 uint64_t ctf_trace_id
;
991 status
= lttng_live_recv(viewer_connection
, &stream
,
993 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
994 viewer_handle_recv_status(self_comp
, NULL
,
995 status
, "stream reply");
998 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
999 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
1000 stream_id
= be64toh(stream
.id
);
1001 ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
1003 if (stream
.metadata_flag
) {
1004 BT_COMP_LOGI(" metadata stream %" PRIu64
" : %s/%s",
1005 stream_id
, stream
.path_name
, stream
.channel_name
);
1006 if (lttng_live_metadata_create_stream(session
,
1007 ctf_trace_id
, stream_id
,
1008 stream
.path_name
)) {
1009 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1010 "Error creating metadata stream");
1011 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1014 session
->lazy_stream_msg_init
= true;
1016 BT_COMP_LOGI(" stream %" PRIu64
" : %s/%s",
1017 stream_id
, stream
.path_name
, stream
.channel_name
);
1018 live_stream
= lttng_live_stream_iterator_create(session
,
1019 ctf_trace_id
, stream_id
, self_msg_iter
);
1021 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1022 "Error creating stream");
1023 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1028 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
1035 enum lttng_live_viewer_status
lttng_live_attach_session(
1036 struct lttng_live_session
*session
,
1037 bt_self_message_iterator
*self_msg_iter
)
1039 struct lttng_viewer_cmd cmd
;
1040 enum lttng_live_viewer_status status
;
1041 struct lttng_viewer_attach_session_request rq
;
1042 struct lttng_viewer_attach_session_response rp
;
1043 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1044 session
->lttng_live_msg_iter
;
1045 struct live_viewer_connection
*viewer_connection
=
1046 lttng_live_msg_iter
->viewer_connection
;
1047 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1048 uint64_t session_id
= session
->id
;
1049 uint32_t streams_count
;
1050 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1051 char cmd_buf
[cmd_buf_len
];
1053 BT_COMP_LOGD("Attaching to session: session-id=%"PRIu64
, session_id
);
1055 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
1056 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1057 cmd
.cmd_version
= htobe32(0);
1059 memset(&rq
, 0, sizeof(rq
));
1060 rq
.session_id
= htobe64(session_id
);
1061 // TODO: add cmd line parameter to select seek beginning
1062 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
1063 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
1066 * Merge the cmd and connection request to prevent a write-write
1067 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1068 * second write to be performed quickly in presence of Nagle's algorithm.
1070 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1071 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1072 status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1073 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1074 viewer_handle_send_status(self_comp
, NULL
,
1075 status
, "attach session command");
1079 status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1080 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1081 viewer_handle_recv_status(self_comp
, NULL
,
1082 status
, "attach session reply");
1086 streams_count
= be32toh(rp
.streams_count
);
1087 switch(be32toh(rp
.status
)) {
1088 case LTTNG_VIEWER_ATTACH_OK
:
1090 case LTTNG_VIEWER_ATTACH_UNK
:
1091 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1092 "Session id %" PRIu64
" is unknown", session_id
);
1093 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1095 case LTTNG_VIEWER_ATTACH_ALREADY
:
1096 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1097 "There is already a viewer attached to this session");
1098 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1100 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
1101 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Not a live session");
1102 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1104 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
1105 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Wrong seek parameter");
1106 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1109 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1110 "Unknown attach return code %u", be32toh(rp
.status
));
1111 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1115 /* We receive the initial list of streams. */
1116 status
= receive_streams(session
, streams_count
, self_msg_iter
);
1118 case LTTNG_LIVE_VIEWER_STATUS_OK
:
1120 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
:
1122 case LTTNG_LIVE_VIEWER_STATUS_ERROR
:
1123 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error receiving streams");
1129 session
->attached
= true;
1130 session
->new_streams_needed
= false;
1137 enum lttng_live_viewer_status
lttng_live_detach_session(
1138 struct lttng_live_session
*session
)
1140 struct lttng_viewer_cmd cmd
;
1141 enum lttng_live_viewer_status status
;
1142 struct lttng_viewer_detach_session_request rq
;
1143 struct lttng_viewer_detach_session_response rp
;
1144 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1145 session
->lttng_live_msg_iter
;
1146 bt_self_component
*self_comp
= session
->self_comp
;
1147 struct live_viewer_connection
*viewer_connection
=
1148 lttng_live_msg_iter
->viewer_connection
;
1149 uint64_t session_id
= session
->id
;
1150 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1151 char cmd_buf
[cmd_buf_len
];
1153 if (!session
->attached
) {
1157 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
1158 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1159 cmd
.cmd_version
= htobe32(0);
1161 memset(&rq
, 0, sizeof(rq
));
1162 rq
.session_id
= htobe64(session_id
);
1165 * Merge the cmd and connection request to prevent a write-write
1166 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1167 * second write to be performed quickly in presence of Nagle's algorithm.
1169 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1170 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1171 status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1172 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1173 viewer_handle_send_status(self_comp
, NULL
,
1174 status
, "detach session command");
1178 status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1179 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1180 viewer_handle_recv_status(self_comp
, NULL
,
1181 status
, "detach session reply");
1185 switch(be32toh(rp
.status
)) {
1186 case LTTNG_VIEWER_DETACH_SESSION_OK
:
1188 case LTTNG_VIEWER_DETACH_SESSION_UNK
:
1189 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
1190 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1192 case LTTNG_VIEWER_DETACH_SESSION_ERR
:
1193 BT_COMP_LOGW("Error detaching session id %" PRIu64
"", session_id
);
1194 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1197 BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp
.status
));
1198 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1202 session
->attached
= false;
1204 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
1211 enum lttng_live_get_one_metadata_status
lttng_live_get_one_metadata_packet(
1212 struct lttng_live_trace
*trace
, FILE *fp
, size_t *reply_len
)
1215 enum lttng_live_get_one_metadata_status status
;
1216 enum lttng_live_viewer_status viewer_status
;
1217 struct lttng_viewer_cmd cmd
;
1218 struct lttng_viewer_get_metadata rq
;
1219 struct lttng_viewer_metadata_packet rp
;
1222 struct lttng_live_session
*session
= trace
->session
;
1223 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1224 session
->lttng_live_msg_iter
;
1225 struct lttng_live_metadata
*metadata
= trace
->metadata
;
1226 struct live_viewer_connection
*viewer_connection
=
1227 lttng_live_msg_iter
->viewer_connection
;
1228 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1229 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1230 char cmd_buf
[cmd_buf_len
];
1232 BT_COMP_LOGD("Requesting new metadata for trace: "
1233 "trace-id=%"PRIu64
", metadata-stream-id=%"PRIu64
,
1234 trace
->id
, metadata
->stream_id
);
1236 rq
.stream_id
= htobe64(metadata
->stream_id
);
1237 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
1238 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1239 cmd
.cmd_version
= htobe32(0);
1242 * Merge the cmd and connection request to prevent a write-write
1243 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1244 * second write to be performed quickly in presence of Nagle's algorithm.
1246 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1247 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1248 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1249 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1250 viewer_handle_send_status(self_comp
, NULL
,
1251 viewer_status
, "get metadata command");
1252 status
= (enum lttng_live_get_one_metadata_status
) viewer_status
;
1256 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1257 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1258 viewer_handle_recv_status(self_comp
, NULL
,
1259 viewer_status
, "get metadata reply");
1260 status
= (enum lttng_live_get_one_metadata_status
) viewer_status
;
1264 switch (be32toh(rp
.status
)) {
1265 case LTTNG_VIEWER_METADATA_OK
:
1266 BT_COMP_LOGD("Received get_metadata response: ok");
1268 case LTTNG_VIEWER_NO_NEW_METADATA
:
1269 BT_COMP_LOGD("Received get_metadata response: no new");
1270 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
;
1272 case LTTNG_VIEWER_METADATA_ERR
:
1274 * The Relayd cannot find this stream id. Maybe its
1275 * gone already. This can happen in short lived UST app
1276 * in a per-pid session.
1278 BT_COMP_LOGD("Received get_metadata response: error");
1279 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
;
1282 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1283 "Received get_metadata response: unknown");
1284 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1288 len
= be64toh(rp
.len
);
1289 BT_COMP_LOGD("Writing %" PRIu64
" bytes to metadata", len
);
1291 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1292 "Erroneous response length");
1293 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1297 data
= calloc(1, len
);
1299 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
1300 "Failed to allocate data buffer", ".");
1301 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1305 viewer_status
= lttng_live_recv(viewer_connection
, data
, len
);
1306 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1307 viewer_handle_recv_status(self_comp
, NULL
,
1308 viewer_status
, "get metadata packet");
1309 status
= (enum lttng_live_get_one_metadata_status
) viewer_status
;
1314 * Write the metadata to the file handle.
1317 ret_len
= fwrite(data
, 1, len
, fp
);
1318 } while (ret_len
< 0 && errno
== EINTR
);
1320 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1321 "Writing in the metadata file stream");
1322 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1325 BT_ASSERT(ret_len
== len
);
1327 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
;
1337 * Assign the fields from a lttng_viewer_index to a packet_index.
1340 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
1341 struct packet_index
*pindex
)
1346 pindex
->offset
= be64toh(lindex
->offset
);
1347 pindex
->packet_size
= be64toh(lindex
->packet_size
);
1348 pindex
->content_size
= be64toh(lindex
->content_size
);
1349 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
1350 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
1351 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
1355 enum lttng_live_iterator_status
lttng_live_get_next_index(
1356 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1357 struct lttng_live_stream_iterator
*stream
,
1358 struct packet_index
*index
)
1360 struct lttng_viewer_cmd cmd
;
1361 struct lttng_viewer_get_next_index rq
;
1362 enum lttng_live_viewer_status viewer_status
;
1363 struct lttng_viewer_index rp
;
1364 enum lttng_live_iterator_status status
;
1365 struct live_viewer_connection
*viewer_connection
=
1366 lttng_live_msg_iter
->viewer_connection
;
1367 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1368 struct lttng_live_trace
*trace
= stream
->trace
;
1369 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1370 char cmd_buf
[cmd_buf_len
];
1371 uint32_t flags
, rp_status
;
1373 BT_COMP_LOGD("Requesting next index for stream: "
1374 "stream-id=%"PRIu64
, stream
->viewer_stream_id
);
1376 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
1377 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1378 cmd
.cmd_version
= htobe32(0);
1381 memset(&rq
, 0, sizeof(rq
));
1382 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1385 * Merge the cmd and connection request to prevent a write-write
1386 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1387 * second write to be performed quickly in presence of Nagle's algorithm.
1389 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1390 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1391 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1392 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1393 viewer_handle_send_status(self_comp
, NULL
,
1394 viewer_status
, "get next index command");
1398 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1399 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1400 viewer_handle_recv_status(self_comp
, NULL
,
1401 viewer_status
, "get next index reply");
1405 flags
= be32toh(rp
.flags
);
1406 rp_status
= be32toh(rp
.status
);
1408 switch (rp_status
) {
1409 case LTTNG_VIEWER_INDEX_INACTIVE
:
1411 uint64_t ctf_stream_class_id
;
1413 BT_COMP_LOGD("Received get_next_index response: inactive");
1414 memset(index
, 0, sizeof(struct packet_index
));
1415 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
1416 stream
->current_inactivity_ts
= index
->ts_cycles
.timestamp_end
;
1417 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1418 if (stream
->ctf_stream_class_id
!= -1ULL) {
1419 BT_ASSERT(stream
->ctf_stream_class_id
==
1420 ctf_stream_class_id
);
1422 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1424 stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT
;
1425 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1428 case LTTNG_VIEWER_INDEX_OK
:
1430 uint64_t ctf_stream_class_id
;
1432 BT_COMP_LOGD("Received get_next_index response: OK");
1433 lttng_index_to_packet_index(&rp
, index
);
1434 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1435 if (stream
->ctf_stream_class_id
!= -1ULL) {
1436 BT_ASSERT(stream
->ctf_stream_class_id
==
1437 ctf_stream_class_id
);
1439 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1442 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_DATA
;
1444 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1445 BT_COMP_LOGD("Received get_next_index response: new metadata needed");
1446 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
1448 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1449 BT_COMP_LOGD("Received get_next_index response: new streams needed");
1450 lttng_live_need_new_streams(lttng_live_msg_iter
);
1452 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1455 case LTTNG_VIEWER_INDEX_RETRY
:
1456 BT_COMP_LOGD("Received get_next_index response: retry");
1457 memset(index
, 0, sizeof(struct packet_index
));
1458 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1459 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1461 case LTTNG_VIEWER_INDEX_HUP
:
1462 BT_COMP_LOGD("Received get_next_index response: stream hung up");
1463 memset(index
, 0, sizeof(struct packet_index
));
1464 index
->offset
= EOF
;
1465 stream
->state
= LTTNG_LIVE_STREAM_EOF
;
1466 stream
->has_stream_hung_up
= true;
1467 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1469 case LTTNG_VIEWER_INDEX_ERR
:
1470 BT_COMP_LOGD("Received get_next_index response: error");
1471 memset(index
, 0, sizeof(struct packet_index
));
1472 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1473 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1476 BT_COMP_LOGD("Received get_next_index response: unknown value");
1477 memset(index
, 0, sizeof(struct packet_index
));
1478 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1479 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1485 status
= viewer_status_to_live_iterator_status(viewer_status
);
1491 enum ctf_msg_iter_medium_status
lttng_live_get_stream_bytes(
1492 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1493 struct lttng_live_stream_iterator
*stream
, uint8_t *buf
,
1494 uint64_t offset
, uint64_t req_len
, uint64_t *recv_len
)
1496 enum ctf_msg_iter_medium_status status
;
1497 enum lttng_live_viewer_status viewer_status
;
1498 struct lttng_viewer_trace_packet rp
;
1499 struct lttng_viewer_cmd cmd
;
1500 struct lttng_viewer_get_packet rq
;
1501 struct live_viewer_connection
*viewer_connection
=
1502 lttng_live_msg_iter
->viewer_connection
;
1503 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1504 struct lttng_live_trace
*trace
= stream
->trace
;
1505 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1506 char cmd_buf
[cmd_buf_len
];
1507 uint32_t flags
, rp_status
;
1509 BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64
", req_len=%" PRIu64
,
1511 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
1512 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1513 cmd
.cmd_version
= htobe32(0);
1515 memset(&rq
, 0, sizeof(rq
));
1516 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1517 rq
.offset
= htobe64(offset
);
1518 rq
.len
= htobe32(req_len
);
1521 * Merge the cmd and connection request to prevent a write-write
1522 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1523 * second write to be performed quickly in presence of Nagle's algorithm.
1525 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1526 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1527 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1528 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1529 viewer_handle_send_status(self_comp
, NULL
,
1530 viewer_status
, "get data packet command");
1534 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1535 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1536 viewer_handle_recv_status(self_comp
, NULL
,
1537 viewer_status
, "get data packet reply");
1541 flags
= be32toh(rp
.flags
);
1542 rp_status
= be32toh(rp
.status
);
1544 switch (rp_status
) {
1545 case LTTNG_VIEWER_GET_PACKET_OK
:
1546 req_len
= be32toh(rp
.len
);
1547 BT_COMP_LOGD("Received get_data_packet response: Ok, "
1548 "packet size : %" PRIu64
"", req_len
);
1549 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
1551 case LTTNG_VIEWER_GET_PACKET_RETRY
:
1552 /* Unimplemented by relay daemon */
1553 BT_COMP_LOGD("Received get_data_packet response: retry");
1554 status
= CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1556 case LTTNG_VIEWER_GET_PACKET_ERR
:
1557 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1558 BT_COMP_LOGD("get_data_packet: new metadata needed, try again later");
1559 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
1561 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1562 BT_COMP_LOGD("get_data_packet: new streams needed, try again later");
1563 lttng_live_need_new_streams(lttng_live_msg_iter
);
1565 if (flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
1566 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
1567 status
= CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1570 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1571 "Received get_data_packet response: error");
1572 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
1574 case LTTNG_VIEWER_GET_PACKET_EOF
:
1575 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
1578 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1579 "Received get_data_packet response: unknown");
1580 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
1585 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
1589 viewer_status
= lttng_live_recv(viewer_connection
, buf
, req_len
);
1590 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1591 viewer_handle_recv_status(self_comp
, NULL
,
1592 viewer_status
, "get data packet");
1595 *recv_len
= req_len
;
1597 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
1601 status
= viewer_status_to_ctf_msg_iter_medium_status(viewer_status
);
1607 * Request new streams for a session.
1610 enum lttng_live_iterator_status
lttng_live_get_new_streams(
1611 struct lttng_live_session
*session
,
1612 bt_self_message_iterator
*self_msg_iter
)
1614 enum lttng_live_iterator_status status
=
1615 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1616 struct lttng_viewer_cmd cmd
;
1617 struct lttng_viewer_new_streams_request rq
;
1618 struct lttng_viewer_new_streams_response rp
;
1619 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1620 session
->lttng_live_msg_iter
;
1621 enum lttng_live_viewer_status viewer_status
;
1622 struct live_viewer_connection
*viewer_connection
=
1623 lttng_live_msg_iter
->viewer_connection
;
1624 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1625 uint32_t streams_count
;
1626 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1627 char cmd_buf
[cmd_buf_len
];
1629 if (!session
->new_streams_needed
) {
1630 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1634 BT_COMP_LOGD("Requesting new streams for session: "
1635 "session-id=%"PRIu64
, session
->id
);
1637 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1638 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1639 cmd
.cmd_version
= htobe32(0);
1641 memset(&rq
, 0, sizeof(rq
));
1642 rq
.session_id
= htobe64(session
->id
);
1645 * Merge the cmd and connection request to prevent a write-write
1646 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1647 * second write to be performed quickly in presence of Nagle's algorithm.
1649 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1650 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1651 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1652 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1653 viewer_handle_send_status(self_comp
, NULL
,
1654 viewer_status
, "get new streams command");
1655 status
= viewer_status_to_live_iterator_status(viewer_status
);
1659 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1660 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1661 viewer_handle_recv_status(self_comp
, NULL
,
1662 viewer_status
, "get new streams reply");
1663 status
= viewer_status_to_live_iterator_status(viewer_status
);
1667 streams_count
= be32toh(rp
.streams_count
);
1669 switch(be32toh(rp
.status
)) {
1670 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1671 session
->new_streams_needed
= false;
1673 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1674 session
->new_streams_needed
= false;
1676 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1677 session
->new_streams_needed
= false;
1678 session
->closed
= true;
1679 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1681 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1682 BT_COMP_LOGD("Received get_new_streams response: error");
1683 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1686 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1687 "Received get_new_streams response: Unknown:"
1688 "return code %u", be32toh(rp
.status
));
1689 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1693 viewer_status
= receive_streams(session
, streams_count
, self_msg_iter
);
1694 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1695 viewer_handle_recv_status(self_comp
, NULL
,
1696 viewer_status
, "new streams");
1697 status
= viewer_status_to_live_iterator_status(viewer_status
);
1701 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1707 enum lttng_live_viewer_status
live_viewer_connection_create(
1708 bt_self_component
*self_comp
,
1709 bt_self_component_class
*self_comp_class
,
1710 bt_logging_level log_level
,
1711 const char *url
, bool in_query
,
1712 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1713 struct live_viewer_connection
**viewer
)
1715 struct live_viewer_connection
*viewer_connection
;
1716 enum lttng_live_viewer_status status
;
1718 viewer_connection
= g_new0(struct live_viewer_connection
, 1);
1720 if (bt_socket_init(log_level
) != 0) {
1721 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
1722 self_comp_class
, "Failed to init socket");
1723 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1727 viewer_connection
->log_level
= log_level
;
1729 viewer_connection
->self_comp
= self_comp
;
1730 viewer_connection
->self_comp_class
= self_comp_class
;
1732 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
1733 viewer_connection
->port
= -1;
1734 viewer_connection
->in_query
= in_query
;
1735 viewer_connection
->lttng_live_msg_iter
= lttng_live_msg_iter
;
1736 viewer_connection
->url
= g_string_new(url
);
1737 if (!viewer_connection
->url
) {
1738 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
1739 self_comp_class
, "Failed to allocate URL buffer");
1740 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1744 BT_COMP_OR_COMP_CLASS_LOGD(self_comp
, self_comp_class
,
1745 "Establishing connection to url \"%s\"...", url
);
1746 status
= lttng_live_connect_viewer(viewer_connection
);
1748 * Only print error and append cause in case of error. not in case of
1751 if (status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1752 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
1753 self_comp_class
, "Failed to establish connection: "
1756 } else if (status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1759 BT_COMP_OR_COMP_CLASS_LOGD(self_comp
, self_comp_class
,
1760 "Connection to url \"%s\" is established", url
);
1762 *viewer
= viewer_connection
;
1763 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
1767 if (viewer_connection
) {
1768 live_viewer_connection_destroy(viewer_connection
);
1775 void live_viewer_connection_destroy(
1776 struct live_viewer_connection
*viewer_connection
)
1778 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1779 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
1781 if (!viewer_connection
) {
1785 BT_COMP_OR_COMP_CLASS_LOGD(self_comp
, self_comp_class
,
1786 "Closing connection to relay:"
1787 "relay-url=\"%s\"", viewer_connection
->url
->str
);
1789 lttng_live_disconnect_viewer(viewer_connection
);
1791 if (viewer_connection
->url
) {
1792 g_string_free(viewer_connection
->url
, true);
1795 if (viewer_connection
->relay_hostname
) {
1796 g_string_free(viewer_connection
->relay_hostname
, true);
1799 if (viewer_connection
->target_hostname
) {
1800 g_string_free(viewer_connection
->target_hostname
, true);
1803 if (viewer_connection
->session_name
) {
1804 g_string_free(viewer_connection
->session_name
, true);
1807 if (viewer_connection
->proto
) {
1808 g_string_free(viewer_connection
->proto
, true);
1811 g_free(viewer_connection
);