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",
285 viewer_connection
->relay_hostname
= lttng_live_url_parts
.hostname
;
286 lttng_live_url_parts
.hostname
= NULL
;
288 if (lttng_live_url_parts
.port
>= 0) {
289 viewer_connection
->port
= lttng_live_url_parts
.port
;
291 viewer_connection
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
294 viewer_connection
->target_hostname
= lttng_live_url_parts
.target_hostname
;
295 lttng_live_url_parts
.target_hostname
= NULL
;
297 if (lttng_live_url_parts
.session_name
) {
298 viewer_connection
->session_name
= lttng_live_url_parts
.session_name
;
299 lttng_live_url_parts
.session_name
= NULL
;
302 BT_COMP_LOGI("Connecting to hostname : %s, port : %d, "
303 "target hostname : %s, session name : %s, proto : %s",
304 viewer_connection
->relay_hostname
->str
,
305 viewer_connection
->port
,
306 !viewer_connection
->target_hostname
?
307 "<none>" : viewer_connection
->target_hostname
->str
,
308 !viewer_connection
->session_name
?
309 "<none>" : viewer_connection
->session_name
->str
,
310 lttng_live_url_parts
.proto
->str
);
314 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
319 enum lttng_live_viewer_status
lttng_live_handshake(
320 struct live_viewer_connection
*viewer_connection
)
322 struct lttng_viewer_cmd cmd
;
323 struct lttng_viewer_connect connect
;
324 enum lttng_live_viewer_status status
;
325 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
326 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
327 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(connect
);
328 char cmd_buf
[cmd_buf_len
];
330 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
331 cmd
.data_size
= htobe64((uint64_t) sizeof(connect
));
332 cmd
.cmd_version
= htobe32(0);
334 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
335 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
336 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
337 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
340 * Merge the cmd and connection request to prevent a write-write
341 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
342 * second write to be performed quickly in presence of Nagle's algorithm
344 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
345 memcpy(cmd_buf
+ sizeof(cmd
), &connect
, sizeof(connect
));
347 status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
348 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
349 viewer_handle_send_status(self_comp
, self_comp_class
,
350 status
, "viewer connect command");
354 status
= lttng_live_recv(viewer_connection
, &connect
, sizeof(connect
));
355 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
356 viewer_handle_recv_status(self_comp
, self_comp_class
,
357 status
, "viewer connect reply");
361 BT_COMP_LOGI("Received viewer session ID : %" PRIu64
,
362 (uint64_t) be64toh(connect
.viewer_session_id
));
363 BT_COMP_LOGI("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 host
= gethostbyname(viewer_connection
->relay_hostname
->str
);
407 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
408 self_comp_class
, "Cannot lookup hostname: hostname=\"%s\"",
409 viewer_connection
->relay_hostname
->str
);
410 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
414 if ((viewer_connection
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == BT_INVALID_SOCKET
) {
415 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
416 self_comp_class
, "Socket creation failed: %s", bt_socket_errormsg());
417 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
421 server_addr
.sin_family
= AF_INET
;
422 server_addr
.sin_port
= htons(viewer_connection
->port
);
423 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
424 memset(&(server_addr
.sin_zero
), 0, 8);
426 if (connect(viewer_connection
->control_sock
, (struct sockaddr
*) &server_addr
,
427 sizeof(struct sockaddr
)) == BT_SOCKET_ERROR
) {
428 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
429 self_comp_class
, "Connection failed: %s",
430 bt_socket_errormsg());
431 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
435 status
= lttng_live_handshake(viewer_connection
);
438 * Only print error and append cause in case of error. not in case of
441 if (status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
442 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
443 self_comp_class
, "Viewer handshake failed");
445 } else if (status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
452 if (viewer_connection
->control_sock
!= BT_INVALID_SOCKET
) {
453 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
454 BT_COMP_OR_COMP_CLASS_LOGE(self_comp
, self_comp_class
,
455 "Error closing socket: %s", bt_socket_errormsg());
458 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
464 void lttng_live_disconnect_viewer(
465 struct live_viewer_connection
*viewer_connection
)
467 if (viewer_connection
->control_sock
== BT_INVALID_SOCKET
) {
470 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
471 BT_COMP_LOGE("Error closing socket: %s",
472 bt_socket_errormsg());
473 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
478 int list_update_session(bt_value
*results
,
479 const struct lttng_viewer_session
*session
,
480 bool *_found
, struct live_viewer_connection
*viewer_connection
)
482 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
483 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
486 bt_value
*map
= NULL
;
487 bt_value
*hostname
= NULL
;
488 bt_value
*session_name
= NULL
;
489 bt_value
*btval
= NULL
;
492 len
= bt_value_array_get_length(results
);
493 for (i
= 0; i
< len
; i
++) {
494 const char *hostname_str
= NULL
;
495 const char *session_name_str
= NULL
;
497 map
= bt_value_array_borrow_element_by_index(results
, i
);
498 hostname
= bt_value_map_borrow_entry_value(map
, "target-hostname");
500 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
502 "Error borrowing \"target-hostname\" entry.");
506 session_name
= bt_value_map_borrow_entry_value(map
, "session-name");
508 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
510 "Error borrowing \"session-name\" entry.");
514 hostname_str
= bt_value_string_get(hostname
);
515 session_name_str
= bt_value_string_get(session_name
);
517 if (strcmp(session
->hostname
, hostname_str
) == 0
518 && strcmp(session
->session_name
, session_name_str
) == 0) {
520 uint32_t streams
= be32toh(session
->streams
);
521 uint32_t clients
= be32toh(session
->clients
);
525 btval
= bt_value_map_borrow_entry_value(map
, "stream-count");
527 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
528 self_comp
, self_comp_class
,
529 "Error borrowing \"stream-count\" entry.");
533 val
= bt_value_integer_unsigned_get(btval
);
536 bt_value_integer_unsigned_set(btval
, val
);
538 btval
= bt_value_map_borrow_entry_value(map
, "client-count");
540 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
541 self_comp
, self_comp_class
,
542 "Error borrowing \"client-count\" entry.");
546 val
= bt_value_integer_unsigned_get(btval
);
548 val
= bt_max_t(int64_t, clients
, val
);
549 bt_value_integer_unsigned_set(btval
, val
);
562 int list_append_session(bt_value
*results
,
564 const struct lttng_viewer_session
*session
,
565 struct live_viewer_connection
*viewer_connection
)
568 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
569 bt_value_map_insert_entry_status insert_status
;
570 bt_value_array_append_element_status append_status
;
571 bt_value
*map
= NULL
;
576 * If the session already exists, add the stream count to it,
577 * and do max of client counts.
579 ret
= list_update_session(results
, session
, &found
, viewer_connection
);
584 map
= bt_value_map_create();
586 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
587 "Error creating map value.");
592 if (base_url
->len
< 1) {
593 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
594 "Error: base_url length smaller than 1.");
602 url
= g_string_new(base_url
->str
);
603 g_string_append(url
, "/host/");
604 g_string_append(url
, session
->hostname
);
605 g_string_append_c(url
, '/');
606 g_string_append(url
, session
->session_name
);
608 insert_status
= bt_value_map_insert_string_entry(map
, "url", url
->str
);
609 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
610 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
611 "Error inserting \"url\" entry.");
617 * key = "target-hostname",
620 insert_status
= bt_value_map_insert_string_entry(map
, "target-hostname",
622 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
623 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
624 "Error inserting \"target-hostname\" entry.");
630 * key = "session-name",
633 insert_status
= bt_value_map_insert_string_entry(map
, "session-name",
634 session
->session_name
);
635 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
636 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
637 "Error inserting \"session-name\" entry.");
647 uint32_t live_timer
= be32toh(session
->live_timer
);
649 insert_status
= bt_value_map_insert_unsigned_integer_entry(
650 map
, "timer-us", live_timer
);
651 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
652 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
653 "Error inserting \"timer-us\" entry.");
660 * key = "stream-count",
664 uint32_t streams
= be32toh(session
->streams
);
666 insert_status
= bt_value_map_insert_unsigned_integer_entry(map
,
667 "stream-count", streams
);
668 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
669 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
670 "Error inserting \"stream-count\" entry.");
677 * key = "client-count",
681 uint32_t clients
= be32toh(session
->clients
);
683 insert_status
= bt_value_map_insert_unsigned_integer_entry(map
,
684 "client-count", clients
);
685 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
686 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
687 "Error inserting \"client-count\" entry.");
693 append_status
= bt_value_array_append_element(results
, map
);
694 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
695 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
696 "Error appending map to results.");
702 g_string_free(url
, true);
704 BT_VALUE_PUT_REF_AND_RESET(map
);
709 * Data structure returned:
720 * key = "target-hostname",
724 * key = "session-name",
732 * key = "stream-count",
736 * key = "client-count",
745 bt_component_class_query_method_status
live_viewer_connection_list_sessions(
746 struct live_viewer_connection
*viewer_connection
,
747 const bt_value
**user_result
)
749 bt_self_component_class
*self_comp_class
= viewer_connection
->self_comp_class
;
750 bt_component_class_query_method_status status
=
751 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
752 bt_value
*result
= NULL
;
753 enum lttng_live_viewer_status viewer_status
;
754 struct lttng_viewer_cmd cmd
;
755 struct lttng_viewer_list_sessions list
;
756 uint32_t i
, sessions_count
;
758 result
= bt_value_array_create();
760 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
761 "Error creating array");
762 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
766 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
767 cmd
.data_size
= htobe64((uint64_t) 0);
768 cmd
.cmd_version
= htobe32(0);
770 viewer_status
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
771 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
772 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
773 "Error sending list sessions command");
774 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
776 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
777 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
781 viewer_status
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
782 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
783 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
784 "Error receiving session list");
785 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
787 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
788 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
792 sessions_count
= be32toh(list
.sessions_count
);
793 for (i
= 0; i
< sessions_count
; i
++) {
794 struct lttng_viewer_session lsession
;
796 viewer_status
= lttng_live_recv(viewer_connection
, &lsession
,
798 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
799 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
800 "Error receiving session:");
801 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
803 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
804 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
808 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
809 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
810 if (list_append_session(result
, viewer_connection
->url
,
811 &lsession
, viewer_connection
)) {
812 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
813 "Error appending session");
814 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
819 *user_result
= result
;
822 BT_VALUE_PUT_REF_AND_RESET(result
);
828 enum lttng_live_viewer_status
lttng_live_query_session_ids(
829 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
831 struct lttng_viewer_cmd cmd
;
832 struct lttng_viewer_list_sessions list
;
833 struct lttng_viewer_session lsession
;
834 uint32_t i
, sessions_count
;
836 enum lttng_live_viewer_status status
;
837 struct live_viewer_connection
*viewer_connection
=
838 lttng_live_msg_iter
->viewer_connection
;
839 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
840 bt_self_component_class
*self_comp_class
=
841 viewer_connection
->self_comp_class
;
843 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
844 cmd
.data_size
= htobe64((uint64_t) 0);
845 cmd
.cmd_version
= htobe32(0);
847 status
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
848 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
849 viewer_handle_send_status(self_comp
, self_comp_class
,
850 status
, "list sessions command");
854 status
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
855 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
856 viewer_handle_recv_status(self_comp
, self_comp_class
,
857 status
, "session list reply");
861 sessions_count
= be32toh(list
.sessions_count
);
862 for (i
= 0; i
< sessions_count
; i
++) {
863 status
= lttng_live_recv(viewer_connection
, &lsession
,
865 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
866 viewer_handle_recv_status(self_comp
, self_comp_class
,
867 status
, "session reply");
870 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
871 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
872 session_id
= be64toh(lsession
.id
);
874 BT_COMP_LOGI("Adding session %" PRIu64
" hostname: %s session_name: %s",
875 session_id
, lsession
.hostname
, lsession
.session_name
);
877 if ((strncmp(lsession
.session_name
,
878 viewer_connection
->session_name
->str
,
879 LTTNG_VIEWER_NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
880 viewer_connection
->target_hostname
->str
,
881 LTTNG_VIEWER_HOST_NAME_MAX
) == 0)) {
882 if (lttng_live_add_session(lttng_live_msg_iter
, session_id
,
884 lsession
.session_name
)) {
885 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
886 "Failed to add live session");
887 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
893 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
900 enum lttng_live_viewer_status
lttng_live_create_viewer_session(
901 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
903 struct lttng_viewer_cmd cmd
;
904 struct lttng_viewer_create_session_response resp
;
905 enum lttng_live_viewer_status status
;
906 struct live_viewer_connection
*viewer_connection
=
907 lttng_live_msg_iter
->viewer_connection
;
908 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
909 bt_self_component_class
*self_comp_class
=
910 viewer_connection
->self_comp_class
;
912 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
913 cmd
.data_size
= htobe64((uint64_t) 0);
914 cmd
.cmd_version
= htobe32(0);
916 status
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
917 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
918 viewer_handle_send_status(self_comp
, self_comp_class
,
919 status
, "create session command");
923 status
= lttng_live_recv(viewer_connection
, &resp
, sizeof(resp
));
924 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
925 viewer_handle_recv_status(self_comp
, self_comp_class
,
926 status
, "create session reply");
930 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
931 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
932 "Error creating viewer session");
933 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
937 status
= lttng_live_query_session_ids(lttng_live_msg_iter
);
938 if (status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
939 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
940 "Failed to query live viewer session ids");
942 } else if (status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
951 enum lttng_live_viewer_status
receive_streams(struct lttng_live_session
*session
,
952 uint32_t stream_count
,
953 bt_self_message_iterator
*self_msg_iter
)
956 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
957 session
->lttng_live_msg_iter
;
958 enum lttng_live_viewer_status status
;
959 struct live_viewer_connection
*viewer_connection
=
960 lttng_live_msg_iter
->viewer_connection
;
961 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
963 BT_COMP_LOGI("Getting %" PRIu32
" new streams:", stream_count
);
964 for (i
= 0; i
< stream_count
; i
++) {
965 struct lttng_viewer_stream stream
;
966 struct lttng_live_stream_iterator
*live_stream
;
968 uint64_t ctf_trace_id
;
970 status
= lttng_live_recv(viewer_connection
, &stream
,
972 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
973 viewer_handle_recv_status(self_comp
, NULL
,
974 status
, "stream reply");
977 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
978 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
979 stream_id
= be64toh(stream
.id
);
980 ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
982 if (stream
.metadata_flag
) {
983 BT_COMP_LOGI(" metadata stream %" PRIu64
" : %s/%s",
984 stream_id
, stream
.path_name
, stream
.channel_name
);
985 if (lttng_live_metadata_create_stream(session
,
986 ctf_trace_id
, stream_id
,
988 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
989 "Error creating metadata stream");
990 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
993 session
->lazy_stream_msg_init
= true;
995 BT_COMP_LOGI(" stream %" PRIu64
" : %s/%s",
996 stream_id
, stream
.path_name
, stream
.channel_name
);
997 live_stream
= lttng_live_stream_iterator_create(session
,
998 ctf_trace_id
, stream_id
, self_msg_iter
);
1000 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1001 "Error creating stream");
1002 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1007 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
1014 enum lttng_live_viewer_status
lttng_live_attach_session(
1015 struct lttng_live_session
*session
,
1016 bt_self_message_iterator
*self_msg_iter
)
1018 struct lttng_viewer_cmd cmd
;
1019 enum lttng_live_viewer_status status
;
1020 struct lttng_viewer_attach_session_request rq
;
1021 struct lttng_viewer_attach_session_response rp
;
1022 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1023 session
->lttng_live_msg_iter
;
1024 struct live_viewer_connection
*viewer_connection
=
1025 lttng_live_msg_iter
->viewer_connection
;
1026 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1027 uint64_t session_id
= session
->id
;
1028 uint32_t streams_count
;
1029 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1030 char cmd_buf
[cmd_buf_len
];
1032 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
1033 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1034 cmd
.cmd_version
= htobe32(0);
1036 memset(&rq
, 0, sizeof(rq
));
1037 rq
.session_id
= htobe64(session_id
);
1038 // TODO: add cmd line parameter to select seek beginning
1039 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
1040 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
1043 * Merge the cmd and connection request to prevent a write-write
1044 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1045 * second write to be performed quickly in presence of Nagle's algorithm.
1047 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1048 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1049 status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1050 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1051 viewer_handle_send_status(self_comp
, NULL
,
1052 status
, "attach session command");
1056 status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1057 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1058 viewer_handle_recv_status(self_comp
, NULL
,
1059 status
, "attach session reply");
1063 streams_count
= be32toh(rp
.streams_count
);
1064 switch(be32toh(rp
.status
)) {
1065 case LTTNG_VIEWER_ATTACH_OK
:
1067 case LTTNG_VIEWER_ATTACH_UNK
:
1068 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1069 "Session id %" PRIu64
" is unknown", session_id
);
1070 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1072 case LTTNG_VIEWER_ATTACH_ALREADY
:
1073 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1074 "There is already a viewer attached to this session");
1075 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1077 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
1078 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Not a live session");
1079 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1081 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
1082 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Wrong seek parameter");
1083 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1086 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1087 "Unknown attach return code %u", be32toh(rp
.status
));
1088 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1092 /* We receive the initial list of streams. */
1093 status
= receive_streams(session
, streams_count
, self_msg_iter
);
1095 case LTTNG_LIVE_VIEWER_STATUS_OK
:
1097 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
:
1099 case LTTNG_LIVE_VIEWER_STATUS_ERROR
:
1100 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error receiving streams");
1106 session
->attached
= true;
1107 session
->new_streams_needed
= false;
1114 enum lttng_live_viewer_status
lttng_live_detach_session(
1115 struct lttng_live_session
*session
)
1117 struct lttng_viewer_cmd cmd
;
1118 enum lttng_live_viewer_status status
;
1119 struct lttng_viewer_detach_session_request rq
;
1120 struct lttng_viewer_detach_session_response rp
;
1121 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1122 session
->lttng_live_msg_iter
;
1123 bt_self_component
*self_comp
= session
->self_comp
;
1124 struct live_viewer_connection
*viewer_connection
=
1125 lttng_live_msg_iter
->viewer_connection
;
1126 uint64_t session_id
= session
->id
;
1127 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1128 char cmd_buf
[cmd_buf_len
];
1130 if (!session
->attached
) {
1134 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
1135 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1136 cmd
.cmd_version
= htobe32(0);
1138 memset(&rq
, 0, sizeof(rq
));
1139 rq
.session_id
= htobe64(session_id
);
1142 * Merge the cmd and connection request to prevent a write-write
1143 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1144 * second write to be performed quickly in presence of Nagle's algorithm.
1146 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1147 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1148 status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1149 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1150 viewer_handle_send_status(self_comp
, NULL
,
1151 status
, "detach session command");
1155 status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1156 if (status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1157 viewer_handle_recv_status(self_comp
, NULL
,
1158 status
, "detach session reply");
1162 switch(be32toh(rp
.status
)) {
1163 case LTTNG_VIEWER_DETACH_SESSION_OK
:
1165 case LTTNG_VIEWER_DETACH_SESSION_UNK
:
1166 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
1167 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1169 case LTTNG_VIEWER_DETACH_SESSION_ERR
:
1170 BT_COMP_LOGW("Error detaching session id %" PRIu64
"", session_id
);
1171 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1174 BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp
.status
));
1175 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1179 session
->attached
= false;
1181 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
1188 enum lttng_live_get_one_metadata_status
lttng_live_get_one_metadata_packet(
1189 struct lttng_live_trace
*trace
, FILE *fp
, size_t *reply_len
)
1192 enum lttng_live_get_one_metadata_status status
;
1193 enum lttng_live_viewer_status viewer_status
;
1194 struct lttng_viewer_cmd cmd
;
1195 struct lttng_viewer_get_metadata rq
;
1196 struct lttng_viewer_metadata_packet rp
;
1199 struct lttng_live_session
*session
= trace
->session
;
1200 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1201 session
->lttng_live_msg_iter
;
1202 struct lttng_live_metadata
*metadata
= trace
->metadata
;
1203 struct live_viewer_connection
*viewer_connection
=
1204 lttng_live_msg_iter
->viewer_connection
;
1205 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1206 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1207 char cmd_buf
[cmd_buf_len
];
1209 rq
.stream_id
= htobe64(metadata
->stream_id
);
1210 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
1211 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1212 cmd
.cmd_version
= htobe32(0);
1215 * Merge the cmd and connection request to prevent a write-write
1216 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1217 * second write to be performed quickly in presence of Nagle's algorithm.
1219 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1220 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1221 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1222 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1223 viewer_handle_send_status(self_comp
, NULL
,
1224 viewer_status
, "get metadata command");
1225 status
= (enum lttng_live_get_one_metadata_status
) viewer_status
;
1229 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1230 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1231 viewer_handle_recv_status(self_comp
, NULL
,
1232 viewer_status
, "get metadata reply");
1233 status
= (enum lttng_live_get_one_metadata_status
) viewer_status
;
1237 switch (be32toh(rp
.status
)) {
1238 case LTTNG_VIEWER_METADATA_OK
:
1239 BT_COMP_LOGD("Received get_metadata response: ok");
1241 case LTTNG_VIEWER_NO_NEW_METADATA
:
1242 BT_COMP_LOGD("Received get_metadata response: no new");
1243 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
;
1245 case LTTNG_VIEWER_METADATA_ERR
:
1247 * The Relayd cannot find this stream id. Maybe its
1248 * gone already. This can happen in short lived UST app
1249 * in a per-pid session.
1251 BT_COMP_LOGD("Received get_metadata response: error");
1252 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
;
1255 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1256 "Received get_metadata response: unknown");
1257 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1261 len
= be64toh(rp
.len
);
1262 BT_COMP_LOGD("Writing %" PRIu64
" bytes to metadata", len
);
1264 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1265 "Erroneous response length");
1266 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1270 data
= calloc(1, len
);
1272 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
1273 "Failed to allocate data buffer", ".");
1274 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1278 viewer_status
= lttng_live_recv(viewer_connection
, data
, len
);
1279 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1280 viewer_handle_recv_status(self_comp
, NULL
,
1281 viewer_status
, "get metadata packet");
1282 status
= (enum lttng_live_get_one_metadata_status
) viewer_status
;
1287 * Write the metadata to the file handle.
1290 ret_len
= fwrite(data
, 1, len
, fp
);
1291 } while (ret_len
< 0 && errno
== EINTR
);
1293 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1294 "Writing in the metadata file stream");
1295 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
;
1298 BT_ASSERT(ret_len
== len
);
1300 status
= LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
;
1310 * Assign the fields from a lttng_viewer_index to a packet_index.
1313 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
1314 struct packet_index
*pindex
)
1319 pindex
->offset
= be64toh(lindex
->offset
);
1320 pindex
->packet_size
= be64toh(lindex
->packet_size
);
1321 pindex
->content_size
= be64toh(lindex
->content_size
);
1322 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
1323 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
1324 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
1328 enum lttng_live_iterator_status
lttng_live_get_next_index(
1329 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1330 struct lttng_live_stream_iterator
*stream
,
1331 struct packet_index
*index
)
1333 struct lttng_viewer_cmd cmd
;
1334 struct lttng_viewer_get_next_index rq
;
1335 enum lttng_live_viewer_status viewer_status
;
1336 struct lttng_viewer_index rp
;
1337 enum lttng_live_iterator_status status
;
1338 struct live_viewer_connection
*viewer_connection
=
1339 lttng_live_msg_iter
->viewer_connection
;
1340 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1341 struct lttng_live_trace
*trace
= stream
->trace
;
1342 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1343 char cmd_buf
[cmd_buf_len
];
1344 uint32_t flags
, rp_status
;
1346 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
1347 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1348 cmd
.cmd_version
= htobe32(0);
1351 memset(&rq
, 0, sizeof(rq
));
1352 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1355 * Merge the cmd and connection request to prevent a write-write
1356 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1357 * second write to be performed quickly in presence of Nagle's algorithm.
1359 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1360 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1361 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1362 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1363 viewer_handle_send_status(self_comp
, NULL
,
1364 viewer_status
, "get next index command");
1368 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1369 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1370 viewer_handle_recv_status(self_comp
, NULL
,
1371 viewer_status
, "get next index reply");
1375 flags
= be32toh(rp
.flags
);
1376 rp_status
= be32toh(rp
.status
);
1378 switch (rp_status
) {
1379 case LTTNG_VIEWER_INDEX_INACTIVE
:
1381 uint64_t ctf_stream_class_id
;
1383 BT_COMP_LOGD("Received get_next_index response: inactive");
1384 memset(index
, 0, sizeof(struct packet_index
));
1385 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
1386 stream
->current_inactivity_ts
= index
->ts_cycles
.timestamp_end
;
1387 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1388 if (stream
->ctf_stream_class_id
!= -1ULL) {
1389 BT_ASSERT(stream
->ctf_stream_class_id
==
1390 ctf_stream_class_id
);
1392 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1394 stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT
;
1395 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1398 case LTTNG_VIEWER_INDEX_OK
:
1400 uint64_t ctf_stream_class_id
;
1402 BT_COMP_LOGD("Received get_next_index response: OK");
1403 lttng_index_to_packet_index(&rp
, index
);
1404 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1405 if (stream
->ctf_stream_class_id
!= -1ULL) {
1406 BT_ASSERT(stream
->ctf_stream_class_id
==
1407 ctf_stream_class_id
);
1409 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1412 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_DATA
;
1414 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1415 BT_COMP_LOGD("Received get_next_index response: new metadata needed");
1416 trace
->new_metadata_needed
= true;
1418 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1419 BT_COMP_LOGD("Received get_next_index response: new streams needed");
1420 lttng_live_need_new_streams(lttng_live_msg_iter
);
1422 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1425 case LTTNG_VIEWER_INDEX_RETRY
:
1426 BT_COMP_LOGD("Received get_next_index response: retry");
1427 memset(index
, 0, sizeof(struct packet_index
));
1428 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1429 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1431 case LTTNG_VIEWER_INDEX_HUP
:
1432 BT_COMP_LOGD("Received get_next_index response: stream hung up");
1433 memset(index
, 0, sizeof(struct packet_index
));
1434 index
->offset
= EOF
;
1435 stream
->state
= LTTNG_LIVE_STREAM_EOF
;
1436 stream
->has_stream_hung_up
= true;
1437 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1439 case LTTNG_VIEWER_INDEX_ERR
:
1440 BT_COMP_LOGD("Received get_next_index response: error");
1441 memset(index
, 0, sizeof(struct packet_index
));
1442 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1443 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1446 BT_COMP_LOGD("Received get_next_index response: unknown value");
1447 memset(index
, 0, sizeof(struct packet_index
));
1448 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1449 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1455 status
= viewer_status_to_live_iterator_status(viewer_status
);
1461 enum ctf_msg_iter_medium_status
lttng_live_get_stream_bytes(
1462 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1463 struct lttng_live_stream_iterator
*stream
, uint8_t *buf
,
1464 uint64_t offset
, uint64_t req_len
, uint64_t *recv_len
)
1466 enum ctf_msg_iter_medium_status status
;
1467 enum lttng_live_viewer_status viewer_status
;
1468 struct lttng_viewer_trace_packet rp
;
1469 struct lttng_viewer_cmd cmd
;
1470 struct lttng_viewer_get_packet rq
;
1471 struct live_viewer_connection
*viewer_connection
=
1472 lttng_live_msg_iter
->viewer_connection
;
1473 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1474 struct lttng_live_trace
*trace
= stream
->trace
;
1475 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1476 char cmd_buf
[cmd_buf_len
];
1477 uint32_t flags
, rp_status
;
1479 BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64
", req_len=%" PRIu64
,
1481 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
1482 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1483 cmd
.cmd_version
= htobe32(0);
1485 memset(&rq
, 0, sizeof(rq
));
1486 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1487 rq
.offset
= htobe64(offset
);
1488 rq
.len
= htobe32(req_len
);
1491 * Merge the cmd and connection request to prevent a write-write
1492 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1493 * second write to be performed quickly in presence of Nagle's algorithm.
1495 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1496 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1497 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1498 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1499 viewer_handle_send_status(self_comp
, NULL
,
1500 viewer_status
, "get data packet command");
1504 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1505 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1506 viewer_handle_recv_status(self_comp
, NULL
,
1507 viewer_status
, "get data packet reply");
1511 flags
= be32toh(rp
.flags
);
1512 rp_status
= be32toh(rp
.status
);
1514 switch (rp_status
) {
1515 case LTTNG_VIEWER_GET_PACKET_OK
:
1516 req_len
= be32toh(rp
.len
);
1517 BT_COMP_LOGD("Received get_data_packet response: Ok, "
1518 "packet size : %" PRIu64
"", req_len
);
1519 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
1521 case LTTNG_VIEWER_GET_PACKET_RETRY
:
1522 /* Unimplemented by relay daemon */
1523 BT_COMP_LOGD("Received get_data_packet response: retry");
1524 status
= CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1526 case LTTNG_VIEWER_GET_PACKET_ERR
:
1527 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1528 BT_COMP_LOGD("get_data_packet: new metadata needed, try again later");
1529 trace
->new_metadata_needed
= true;
1531 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1532 BT_COMP_LOGD("get_data_packet: new streams needed, try again later");
1533 lttng_live_need_new_streams(lttng_live_msg_iter
);
1535 if (flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
1536 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
1537 status
= CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1540 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1541 "Received get_data_packet response: error");
1542 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
1544 case LTTNG_VIEWER_GET_PACKET_EOF
:
1545 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
1548 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1549 "Received get_data_packet response: unknown");
1550 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
1555 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
1559 viewer_status
= lttng_live_recv(viewer_connection
, buf
, req_len
);
1560 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1561 viewer_handle_recv_status(self_comp
, NULL
,
1562 viewer_status
, "get data packet");
1565 *recv_len
= req_len
;
1567 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
1571 status
= viewer_status_to_ctf_msg_iter_medium_status(viewer_status
);
1577 * Request new streams for a session.
1580 enum lttng_live_iterator_status
lttng_live_get_new_streams(
1581 struct lttng_live_session
*session
,
1582 bt_self_message_iterator
*self_msg_iter
)
1584 enum lttng_live_iterator_status status
=
1585 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1586 struct lttng_viewer_cmd cmd
;
1587 struct lttng_viewer_new_streams_request rq
;
1588 struct lttng_viewer_new_streams_response rp
;
1589 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1590 session
->lttng_live_msg_iter
;
1591 enum lttng_live_viewer_status viewer_status
;
1592 struct live_viewer_connection
*viewer_connection
=
1593 lttng_live_msg_iter
->viewer_connection
;
1594 bt_self_component
*self_comp
= viewer_connection
->self_comp
;
1595 uint32_t streams_count
;
1596 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1597 char cmd_buf
[cmd_buf_len
];
1599 if (!session
->new_streams_needed
) {
1600 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1604 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1605 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1606 cmd
.cmd_version
= htobe32(0);
1608 memset(&rq
, 0, sizeof(rq
));
1609 rq
.session_id
= htobe64(session
->id
);
1612 * Merge the cmd and connection request to prevent a write-write
1613 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1614 * second write to be performed quickly in presence of Nagle's algorithm.
1616 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1617 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1618 viewer_status
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1619 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1620 viewer_handle_send_status(self_comp
, NULL
,
1621 viewer_status
, "get new streams command");
1622 status
= viewer_status_to_live_iterator_status(viewer_status
);
1626 viewer_status
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1627 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1628 viewer_handle_recv_status(self_comp
, NULL
,
1629 viewer_status
, "get new streams reply");
1630 status
= viewer_status_to_live_iterator_status(viewer_status
);
1634 streams_count
= be32toh(rp
.streams_count
);
1636 switch(be32toh(rp
.status
)) {
1637 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1638 session
->new_streams_needed
= false;
1640 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1641 session
->new_streams_needed
= false;
1643 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1644 session
->new_streams_needed
= false;
1645 session
->closed
= true;
1646 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1648 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1649 BT_COMP_LOGD("Received get_new_streams response: error");
1650 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1653 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1654 "Received get_new_streams response: Unknown:"
1655 "return code %u", be32toh(rp
.status
));
1656 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1660 viewer_status
= receive_streams(session
, streams_count
, self_msg_iter
);
1661 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1662 viewer_handle_recv_status(self_comp
, NULL
,
1663 viewer_status
, "new streams");
1664 status
= viewer_status_to_live_iterator_status(viewer_status
);
1668 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1674 enum lttng_live_viewer_status
live_viewer_connection_create(
1675 bt_self_component
*self_comp
,
1676 bt_self_component_class
*self_comp_class
,
1677 bt_logging_level log_level
,
1678 const char *url
, bool in_query
,
1679 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1680 struct live_viewer_connection
**viewer
)
1682 struct live_viewer_connection
*viewer_connection
;
1683 enum lttng_live_viewer_status status
;
1685 viewer_connection
= g_new0(struct live_viewer_connection
, 1);
1687 if (bt_socket_init(log_level
) != 0) {
1688 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
1689 self_comp_class
, "Failed to init socket");
1690 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1694 viewer_connection
->log_level
= log_level
;
1696 viewer_connection
->self_comp
= self_comp
;
1697 viewer_connection
->self_comp_class
= self_comp_class
;
1699 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
1700 viewer_connection
->port
= -1;
1701 viewer_connection
->in_query
= in_query
;
1702 viewer_connection
->lttng_live_msg_iter
= lttng_live_msg_iter
;
1703 viewer_connection
->url
= g_string_new(url
);
1704 if (!viewer_connection
->url
) {
1705 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
1706 self_comp_class
, "Failed to allocate URL buffer");
1707 status
= LTTNG_LIVE_VIEWER_STATUS_ERROR
;
1711 BT_COMP_LOGI("Establishing connection to url \"%s\"...", url
);
1712 status
= lttng_live_connect_viewer(viewer_connection
);
1714 * Only print error and append cause in case of error. not in case of
1717 if (status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1718 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp
,
1719 self_comp_class
, "Failed to establish connection: "
1722 } else if (status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1725 BT_COMP_LOGI("Connection to url \"%s\" is established", url
);
1727 *viewer
= viewer_connection
;
1728 status
= LTTNG_LIVE_VIEWER_STATUS_OK
;
1732 if (viewer_connection
) {
1733 live_viewer_connection_destroy(viewer_connection
);
1740 void live_viewer_connection_destroy(
1741 struct live_viewer_connection
*viewer_connection
)
1743 BT_COMP_LOGI("Closing connection to url \"%s\"", viewer_connection
->url
->str
);
1745 if (!viewer_connection
) {
1749 lttng_live_disconnect_viewer(viewer_connection
);
1751 if (viewer_connection
->url
) {
1752 g_string_free(viewer_connection
->url
, true);
1755 if (viewer_connection
->relay_hostname
) {
1756 g_string_free(viewer_connection
->relay_hostname
, true);
1759 if (viewer_connection
->target_hostname
) {
1760 g_string_free(viewer_connection
->target_hostname
, true);
1763 if (viewer_connection
->session_name
) {
1764 g_string_free(viewer_connection
->session_name
, true);
1767 g_free(viewer_connection
);