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"
36 #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"
52 ssize_t
lttng_live_recv(struct live_viewer_connection
*viewer_connection
,
53 void *buf
, size_t len
)
56 size_t copied
= 0, to_copy
= len
;
57 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
58 viewer_connection
->lttng_live_msg_iter
;
59 BT_SOCKET sock
= viewer_connection
->control_sock
;
62 ret
= bt_socket_recv(sock
, buf
+ copied
, to_copy
, 0);
64 BT_ASSERT(ret
<= to_copy
);
68 if (ret
== BT_SOCKET_ERROR
&& bt_socket_interrupted()) {
69 if (!viewer_connection
->in_query
&&
70 lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
76 } while (ret
> 0 && to_copy
> 0);
82 /* ret = 0 means orderly shutdown, ret == BT_SOCKET_ERROR is error. */
87 ssize_t
lttng_live_send(struct live_viewer_connection
*viewer_connection
,
88 const void *buf
, size_t len
)
90 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
91 viewer_connection
->lttng_live_msg_iter
;
92 BT_SOCKET sock
= viewer_connection
->control_sock
;
96 ret
= bt_socket_send_nosigpipe(sock
, buf
, len
);
97 if (ret
== BT_SOCKET_ERROR
&& bt_socket_interrupted()) {
98 if (!viewer_connection
->in_query
&&
99 lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
112 int parse_url(struct live_viewer_connection
*viewer_connection
)
114 char error_buf
[256] = { 0 };
115 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
117 const char *path
= viewer_connection
->url
->str
;
123 lttng_live_url_parts
= bt_common_parse_lttng_live_url(path
, error_buf
,
125 if (!lttng_live_url_parts
.proto
) {
126 BT_COMP_LOGW("Invalid LTTng live URL format: %s", error_buf
);
130 viewer_connection
->relay_hostname
= lttng_live_url_parts
.hostname
;
131 lttng_live_url_parts
.hostname
= NULL
;
133 if (lttng_live_url_parts
.port
>= 0) {
134 viewer_connection
->port
= lttng_live_url_parts
.port
;
136 viewer_connection
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
139 viewer_connection
->target_hostname
= lttng_live_url_parts
.target_hostname
;
140 lttng_live_url_parts
.target_hostname
= NULL
;
142 if (lttng_live_url_parts
.session_name
) {
143 viewer_connection
->session_name
= lttng_live_url_parts
.session_name
;
144 lttng_live_url_parts
.session_name
= NULL
;
147 BT_COMP_LOGI("Connecting to hostname : %s, port : %d, "
148 "target hostname : %s, session name : %s, proto : %s",
149 viewer_connection
->relay_hostname
->str
,
150 viewer_connection
->port
,
151 !viewer_connection
->target_hostname
?
152 "<none>" : viewer_connection
->target_hostname
->str
,
153 !viewer_connection
->session_name
?
154 "<none>" : viewer_connection
->session_name
->str
,
155 lttng_live_url_parts
.proto
->str
);
159 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
164 int lttng_live_handshake(struct live_viewer_connection
*viewer_connection
)
166 struct lttng_viewer_cmd cmd
;
167 struct lttng_viewer_connect connect
;
168 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(connect
);
169 char cmd_buf
[cmd_buf_len
];
173 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
174 cmd
.data_size
= htobe64((uint64_t) sizeof(connect
));
175 cmd
.cmd_version
= htobe32(0);
177 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
178 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
179 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
180 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
183 * Merge the cmd and connection request to prevent a write-write
184 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
185 * second write to be performed quickly in presence of Nagle's algorithm
187 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
188 memcpy(cmd_buf
+ sizeof(cmd
), &connect
, sizeof(connect
));
189 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
190 if (ret_len
== BT_SOCKET_ERROR
) {
191 BT_COMP_LOGE("Error sending version: %s", bt_socket_errormsg());
195 BT_ASSERT(ret_len
== cmd_buf_len
);
197 ret_len
= lttng_live_recv(viewer_connection
, &connect
, sizeof(connect
));
199 BT_COMP_LOGI("Remote side has closed connection");
202 if (ret_len
== BT_SOCKET_ERROR
) {
203 BT_COMP_LOGE("Error receiving version: %s", bt_socket_errormsg());
206 BT_ASSERT(ret_len
== sizeof(connect
));
208 BT_COMP_LOGI("Received viewer session ID : %" PRIu64
,
209 (uint64_t) be64toh(connect
.viewer_session_id
));
210 BT_COMP_LOGI("Relayd version : %u.%u", be32toh(connect
.major
),
211 be32toh(connect
.minor
));
213 if (LTTNG_LIVE_MAJOR
!= be32toh(connect
.major
)) {
214 BT_COMP_LOGE("Incompatible lttng-relayd protocol");
217 /* Use the smallest protocol version implemented. */
218 if (LTTNG_LIVE_MINOR
> be32toh(connect
.minor
)) {
219 viewer_connection
->minor
= be32toh(connect
.minor
);
221 viewer_connection
->minor
= LTTNG_LIVE_MINOR
;
223 viewer_connection
->major
= LTTNG_LIVE_MAJOR
;
228 BT_COMP_LOGE("Unable to establish connection");
233 int lttng_live_connect_viewer(struct live_viewer_connection
*viewer_connection
)
235 struct hostent
*host
;
236 struct sockaddr_in server_addr
;
239 if (parse_url(viewer_connection
)) {
243 host
= gethostbyname(viewer_connection
->relay_hostname
->str
);
245 BT_COMP_LOGE("Cannot lookup hostname %s",
246 viewer_connection
->relay_hostname
->str
);
250 if ((viewer_connection
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == BT_INVALID_SOCKET
) {
251 BT_COMP_LOGE("Socket creation failed: %s", bt_socket_errormsg());
255 server_addr
.sin_family
= AF_INET
;
256 server_addr
.sin_port
= htons(viewer_connection
->port
);
257 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
258 memset(&(server_addr
.sin_zero
), 0, 8);
260 if (connect(viewer_connection
->control_sock
, (struct sockaddr
*) &server_addr
,
261 sizeof(struct sockaddr
)) == BT_SOCKET_ERROR
) {
262 BT_COMP_LOGE("Connection failed: %s", bt_socket_errormsg());
265 if (lttng_live_handshake(viewer_connection
)) {
274 if (viewer_connection
->control_sock
!= BT_INVALID_SOCKET
) {
275 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
276 BT_COMP_LOGE("Close: %s", bt_socket_errormsg());
279 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
284 void lttng_live_disconnect_viewer(
285 struct live_viewer_connection
*viewer_connection
)
287 if (viewer_connection
->control_sock
== BT_INVALID_SOCKET
) {
290 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
291 BT_COMP_LOGE("Close: %s", bt_socket_errormsg());
292 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
297 void connection_release(bt_object
*obj
)
299 struct live_viewer_connection
*conn
=
300 container_of(obj
, struct live_viewer_connection
, obj
);
302 live_viewer_connection_destroy(conn
);
306 int list_update_session(bt_value
*results
,
307 const struct lttng_viewer_session
*session
,
308 bool *_found
, struct live_viewer_connection
*viewer_connection
)
312 bt_value
*map
= NULL
;
313 bt_value
*hostname
= NULL
;
314 bt_value
*session_name
= NULL
;
315 bt_value
*btval
= NULL
;
318 len
= bt_value_array_get_length(results
);
319 for (i
= 0; i
< len
; i
++) {
320 const char *hostname_str
= NULL
;
321 const char *session_name_str
= NULL
;
323 map
= bt_value_array_borrow_element_by_index(results
, i
);
324 hostname
= bt_value_map_borrow_entry_value(map
, "target-hostname");
326 BT_COMP_LOGE_STR("Error borrowing \"target-hostname\" entry.");
330 session_name
= bt_value_map_borrow_entry_value(map
, "session-name");
332 BT_COMP_LOGE_STR("Error borrowing \"session-name\" entry.");
336 hostname_str
= bt_value_string_get(hostname
);
337 session_name_str
= bt_value_string_get(session_name
);
339 if (strcmp(session
->hostname
, hostname_str
) == 0
340 && strcmp(session
->session_name
, session_name_str
) == 0) {
342 uint32_t streams
= be32toh(session
->streams
);
343 uint32_t clients
= be32toh(session
->clients
);
347 btval
= bt_value_map_borrow_entry_value(map
, "stream-count");
349 BT_COMP_LOGE_STR("Error borrowing \"stream-count\" entry.");
353 val
= bt_value_integer_unsigned_get(btval
);
356 bt_value_integer_unsigned_set(btval
, val
);
358 btval
= bt_value_map_borrow_entry_value(map
, "client-count");
360 BT_COMP_LOGE_STR("Error borrowing \"client-count\" entry.");
364 val
= bt_value_integer_unsigned_get(btval
);
366 val
= bt_max_t(int64_t, clients
, val
);
367 bt_value_integer_unsigned_set(btval
, val
);
380 int list_append_session(bt_value
*results
,
382 const struct lttng_viewer_session
*session
,
383 struct live_viewer_connection
*viewer_connection
)
386 bt_value_map_insert_entry_status insert_status
;
387 bt_value_array_append_element_status append_status
;
388 bt_value
*map
= NULL
;
393 * If the session already exists, add the stream count to it,
394 * and do max of client counts.
396 ret
= list_update_session(results
, session
, &found
, viewer_connection
);
401 map
= bt_value_map_create();
403 BT_COMP_LOGE_STR("Error creating map value.");
408 if (base_url
->len
< 1) {
409 BT_COMP_LOGE_STR("Error: base_url length smaller than 1.");
417 url
= g_string_new(base_url
->str
);
418 g_string_append(url
, "/host/");
419 g_string_append(url
, session
->hostname
);
420 g_string_append_c(url
, '/');
421 g_string_append(url
, session
->session_name
);
423 insert_status
= bt_value_map_insert_string_entry(map
, "url", url
->str
);
424 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
425 BT_COMP_LOGE_STR("Error inserting \"url\" entry.");
431 * key = "target-hostname",
434 insert_status
= bt_value_map_insert_string_entry(map
, "target-hostname",
436 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
437 BT_COMP_LOGE_STR("Error inserting \"target-hostname\" entry.");
443 * key = "session-name",
446 insert_status
= bt_value_map_insert_string_entry(map
, "session-name",
447 session
->session_name
);
448 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
449 BT_COMP_LOGE_STR("Error inserting \"session-name\" entry.");
459 uint32_t live_timer
= be32toh(session
->live_timer
);
461 insert_status
= bt_value_map_insert_unsigned_integer_entry(
462 map
, "timer-us", live_timer
);
463 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
464 BT_COMP_LOGE_STR("Error inserting \"timer-us\" entry.");
471 * key = "stream-count",
475 uint32_t streams
= be32toh(session
->streams
);
477 insert_status
= bt_value_map_insert_unsigned_integer_entry(map
,
478 "stream-count", streams
);
479 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
480 BT_COMP_LOGE_STR("Error inserting \"stream-count\" entry.");
487 * key = "client-count",
491 uint32_t clients
= be32toh(session
->clients
);
493 insert_status
= bt_value_map_insert_unsigned_integer_entry(map
,
494 "client-count", clients
);
495 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
496 BT_COMP_LOGE_STR("Error inserting \"client-count\" entry.");
502 append_status
= bt_value_array_append_element(results
, map
);
503 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
504 BT_COMP_LOGE_STR("Error appending map to results.");
510 g_string_free(url
, true);
512 BT_VALUE_PUT_REF_AND_RESET(map
);
517 * Data structure returned:
528 * key = "target-hostname",
532 * key = "session-name",
540 * key = "stream-count",
544 * key = "client-count",
553 bt_component_class_query_method_status
live_viewer_connection_list_sessions(
554 struct live_viewer_connection
*viewer_connection
,
555 const bt_value
**user_result
)
557 bt_component_class_query_method_status status
=
558 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
559 bt_value
*result
= NULL
;
560 struct lttng_viewer_cmd cmd
;
561 struct lttng_viewer_list_sessions list
;
562 uint32_t i
, sessions_count
;
565 result
= bt_value_array_create();
567 BT_COMP_LOGE("Error creating array");
568 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
572 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
573 cmd
.data_size
= htobe64((uint64_t) 0);
574 cmd
.cmd_version
= htobe32(0);
576 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
577 if (ret_len
== BT_SOCKET_ERROR
) {
578 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
579 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
582 BT_ASSERT(ret_len
== sizeof(cmd
));
584 ret_len
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
586 BT_COMP_LOGI("Remote side has closed connection");
587 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
590 if (ret_len
== BT_SOCKET_ERROR
) {
591 BT_COMP_LOGE("Error receiving session list: %s", bt_socket_errormsg());
592 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
595 BT_ASSERT(ret_len
== sizeof(list
));
597 sessions_count
= be32toh(list
.sessions_count
);
598 for (i
= 0; i
< sessions_count
; i
++) {
599 struct lttng_viewer_session lsession
;
601 ret_len
= lttng_live_recv(viewer_connection
, &lsession
,
604 BT_COMP_LOGI("Remote side has closed connection");
605 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
608 if (ret_len
== BT_SOCKET_ERROR
) {
609 BT_COMP_LOGE("Error receiving session: %s", bt_socket_errormsg());
610 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
613 BT_ASSERT(ret_len
== sizeof(lsession
));
614 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
615 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
616 if (list_append_session(result
, viewer_connection
->url
,
617 &lsession
, viewer_connection
)) {
618 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
623 *user_result
= result
;
626 BT_VALUE_PUT_REF_AND_RESET(result
);
632 int lttng_live_query_session_ids(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
634 struct lttng_viewer_cmd cmd
;
635 struct lttng_viewer_list_sessions list
;
636 struct lttng_viewer_session lsession
;
637 uint32_t i
, sessions_count
;
640 struct live_viewer_connection
*viewer_connection
=
641 lttng_live_msg_iter
->viewer_connection
;
643 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
644 cmd
.data_size
= htobe64((uint64_t) 0);
645 cmd
.cmd_version
= htobe32(0);
647 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
648 if (ret_len
== BT_SOCKET_ERROR
) {
649 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
652 BT_ASSERT(ret_len
== sizeof(cmd
));
654 ret_len
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
656 BT_COMP_LOGI("Remote side has closed connection");
659 if (ret_len
== BT_SOCKET_ERROR
) {
660 BT_COMP_LOGE("Error receiving session list: %s", bt_socket_errormsg());
663 BT_ASSERT(ret_len
== sizeof(list
));
665 sessions_count
= be32toh(list
.sessions_count
);
666 for (i
= 0; i
< sessions_count
; i
++) {
667 ret_len
= lttng_live_recv(viewer_connection
,
668 &lsession
, sizeof(lsession
));
670 BT_COMP_LOGI("Remote side has closed connection");
673 if (ret_len
== BT_SOCKET_ERROR
) {
674 BT_COMP_LOGE("Error receiving session: %s", bt_socket_errormsg());
677 BT_ASSERT(ret_len
== sizeof(lsession
));
678 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
679 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
680 session_id
= be64toh(lsession
.id
);
682 BT_COMP_LOGI("Adding session %" PRIu64
" hostname: %s session_name: %s",
683 session_id
, lsession
.hostname
, lsession
.session_name
);
685 if ((strncmp(lsession
.session_name
,
686 viewer_connection
->session_name
->str
,
687 LTTNG_VIEWER_NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
688 viewer_connection
->target_hostname
->str
,
689 LTTNG_VIEWER_HOST_NAME_MAX
) == 0)) {
690 if (lttng_live_add_session(lttng_live_msg_iter
, session_id
,
692 lsession
.session_name
)) {
701 BT_COMP_LOGE("Unable to query session ids");
706 int lttng_live_create_viewer_session(
707 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
709 struct lttng_viewer_cmd cmd
;
710 struct lttng_viewer_create_session_response resp
;
712 struct live_viewer_connection
*viewer_connection
=
713 lttng_live_msg_iter
->viewer_connection
;
715 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
716 cmd
.data_size
= htobe64((uint64_t) 0);
717 cmd
.cmd_version
= htobe32(0);
719 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
720 if (ret_len
== BT_SOCKET_ERROR
) {
721 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
724 BT_ASSERT(ret_len
== sizeof(cmd
));
726 ret_len
= lttng_live_recv(viewer_connection
, &resp
, sizeof(resp
));
728 BT_COMP_LOGI("Remote side has closed connection");
731 if (ret_len
== BT_SOCKET_ERROR
) {
732 BT_COMP_LOGE("Error receiving create session reply: %s", bt_socket_errormsg());
735 BT_ASSERT(ret_len
== sizeof(resp
));
737 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
738 BT_COMP_LOGE("Error creating viewer session");
741 if (lttng_live_query_session_ids(lttng_live_msg_iter
)) {
752 int receive_streams(struct lttng_live_session
*session
,
753 uint32_t stream_count
)
757 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
758 session
->lttng_live_msg_iter
;
759 struct live_viewer_connection
*viewer_connection
=
760 lttng_live_msg_iter
->viewer_connection
;
762 BT_COMP_LOGI("Getting %" PRIu32
" new streams:", stream_count
);
763 for (i
= 0; i
< stream_count
; i
++) {
764 struct lttng_viewer_stream stream
;
765 struct lttng_live_stream_iterator
*live_stream
;
767 uint64_t ctf_trace_id
;
769 ret_len
= lttng_live_recv(viewer_connection
, &stream
, sizeof(stream
));
771 BT_COMP_LOGI("Remote side has closed connection");
774 if (ret_len
== BT_SOCKET_ERROR
) {
775 BT_COMP_LOGE("Error receiving stream");
778 BT_ASSERT(ret_len
== sizeof(stream
));
779 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
780 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
781 stream_id
= be64toh(stream
.id
);
782 ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
784 if (stream
.metadata_flag
) {
785 BT_COMP_LOGI(" metadata stream %" PRIu64
" : %s/%s",
786 stream_id
, stream
.path_name
, stream
.channel_name
);
787 if (lttng_live_metadata_create_stream(session
,
788 ctf_trace_id
, stream_id
,
790 BT_COMP_LOGE("Error creating metadata stream");
794 session
->lazy_stream_msg_init
= true;
796 BT_COMP_LOGI(" stream %" PRIu64
" : %s/%s",
797 stream_id
, stream
.path_name
, stream
.channel_name
);
798 live_stream
= lttng_live_stream_iterator_create(session
,
799 ctf_trace_id
, stream_id
);
801 BT_COMP_LOGE("Error creating streamn");
813 int lttng_live_attach_session(struct lttng_live_session
*session
)
815 struct lttng_viewer_cmd cmd
;
816 struct lttng_viewer_attach_session_request rq
;
817 struct lttng_viewer_attach_session_response rp
;
818 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
819 session
->lttng_live_msg_iter
;
820 struct live_viewer_connection
*viewer_connection
=
821 lttng_live_msg_iter
->viewer_connection
;
822 uint64_t session_id
= session
->id
;
823 uint32_t streams_count
;
824 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
825 char cmd_buf
[cmd_buf_len
];
828 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
829 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
830 cmd
.cmd_version
= htobe32(0);
832 memset(&rq
, 0, sizeof(rq
));
833 rq
.session_id
= htobe64(session_id
);
834 // TODO: add cmd line parameter to select seek beginning
835 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
836 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
839 * Merge the cmd and connection request to prevent a write-write
840 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
841 * second write to be performed quickly in presence of Nagle's algorithm.
843 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
844 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
845 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
846 if (ret_len
== BT_SOCKET_ERROR
) {
847 BT_COMP_LOGE("Error sending attach request: %s", bt_socket_errormsg());
851 BT_ASSERT(ret_len
== cmd_buf_len
);
852 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
854 BT_COMP_LOGI("Remote side has closed connection");
857 if (ret_len
== BT_SOCKET_ERROR
) {
858 BT_COMP_LOGE("Error receiving attach response: %s", bt_socket_errormsg());
861 BT_ASSERT(ret_len
== sizeof(rp
));
863 streams_count
= be32toh(rp
.streams_count
);
864 switch(be32toh(rp
.status
)) {
865 case LTTNG_VIEWER_ATTACH_OK
:
867 case LTTNG_VIEWER_ATTACH_UNK
:
868 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
870 case LTTNG_VIEWER_ATTACH_ALREADY
:
871 BT_COMP_LOGW("There is already a viewer attached to this session");
873 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
874 BT_COMP_LOGW("Not a live session");
876 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
877 BT_COMP_LOGE("Wrong seek parameter");
880 BT_COMP_LOGE("Unknown attach return code %u", be32toh(rp
.status
));
884 /* We receive the initial list of streams. */
885 if (receive_streams(session
, streams_count
)) {
889 session
->attached
= true;
890 session
->new_streams_needed
= false;
899 int lttng_live_detach_session(struct lttng_live_session
*session
)
901 struct lttng_viewer_cmd cmd
;
902 struct lttng_viewer_detach_session_request rq
;
903 struct lttng_viewer_detach_session_response rp
;
905 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
906 session
->lttng_live_msg_iter
;
907 struct live_viewer_connection
*viewer_connection
=
908 lttng_live_msg_iter
->viewer_connection
;
909 uint64_t session_id
= session
->id
;
910 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
911 char cmd_buf
[cmd_buf_len
];
913 if (!session
->attached
) {
917 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
918 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
919 cmd
.cmd_version
= htobe32(0);
921 memset(&rq
, 0, sizeof(rq
));
922 rq
.session_id
= htobe64(session_id
);
925 * Merge the cmd and connection request to prevent a write-write
926 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
927 * second write to be performed quickly in presence of Nagle's algorithm.
929 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
930 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
931 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
932 if (ret_len
== BT_SOCKET_ERROR
) {
933 BT_COMP_LOGE("Error sending detach request: %s", bt_socket_errormsg());
937 BT_ASSERT(ret_len
== cmd_buf_len
);
938 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
940 BT_COMP_LOGI("Remote side has closed connection");
943 if (ret_len
== BT_SOCKET_ERROR
) {
944 BT_COMP_LOGE("Error receiving detach response: %s", bt_socket_errormsg());
947 BT_ASSERT(ret_len
== sizeof(rp
));
949 switch(be32toh(rp
.status
)) {
950 case LTTNG_VIEWER_DETACH_SESSION_OK
:
952 case LTTNG_VIEWER_DETACH_SESSION_UNK
:
953 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
955 case LTTNG_VIEWER_DETACH_SESSION_ERR
:
956 BT_COMP_LOGW("Error detaching session id %" PRIu64
"", session_id
);
959 BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp
.status
));
963 session
->attached
= false;
972 ssize_t
lttng_live_get_one_metadata_packet(struct lttng_live_trace
*trace
,
977 struct lttng_viewer_cmd cmd
;
978 struct lttng_viewer_get_metadata rq
;
979 struct lttng_viewer_metadata_packet rp
;
982 struct lttng_live_session
*session
= trace
->session
;
983 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
984 session
->lttng_live_msg_iter
;
985 struct lttng_live_metadata
*metadata
= trace
->metadata
;
986 struct live_viewer_connection
*viewer_connection
=
987 lttng_live_msg_iter
->viewer_connection
;
988 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
989 char cmd_buf
[cmd_buf_len
];
991 rq
.stream_id
= htobe64(metadata
->stream_id
);
992 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
993 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
994 cmd
.cmd_version
= htobe32(0);
997 * Merge the cmd and connection request to prevent a write-write
998 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
999 * second write to be performed quickly in presence of Nagle's algorithm.
1001 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1002 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1003 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1004 if (ret_len
== BT_SOCKET_ERROR
) {
1005 BT_COMP_LOGE("Error sending get_metadata request: %s", bt_socket_errormsg());
1009 BT_ASSERT(ret_len
== cmd_buf_len
);
1010 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1012 BT_COMP_LOGI("Remote side has closed connection");
1015 if (ret_len
== BT_SOCKET_ERROR
) {
1016 BT_COMP_LOGE("Error receiving get_metadata response: %s", bt_socket_errormsg());
1019 BT_ASSERT(ret_len
== sizeof(rp
));
1021 switch (be32toh(rp
.status
)) {
1022 case LTTNG_VIEWER_METADATA_OK
:
1023 BT_COMP_LOGD("get_metadata : OK");
1025 case LTTNG_VIEWER_NO_NEW_METADATA
:
1026 BT_COMP_LOGD("get_metadata : NO NEW");
1029 case LTTNG_VIEWER_METADATA_ERR
:
1030 BT_COMP_LOGD("get_metadata : ERR");
1033 BT_COMP_LOGD("get_metadata : UNKNOWN");
1037 len
= be64toh(rp
.len
);
1038 BT_COMP_LOGD("Writing %" PRIu64
" bytes to metadata", len
);
1043 data
= calloc(1, len
);
1045 BT_COMP_LOGE("relay data calloc: %s", strerror(errno
));
1048 ret_len
= lttng_live_recv(viewer_connection
, data
, len
);
1050 BT_COMP_LOGI("Remote side has closed connection");
1051 goto error_free_data
;
1053 if (ret_len
== BT_SOCKET_ERROR
) {
1054 BT_COMP_LOGE("Error receiving trace packet: %s", bt_socket_errormsg());
1055 goto error_free_data
;
1057 BT_ASSERT(ret_len
== len
);
1060 ret_len
= fwrite(data
, 1, len
, fp
);
1061 } while (ret_len
< 0 && errno
== EINTR
);
1063 BT_COMP_LOGE("Writing in the metadata fp");
1064 goto error_free_data
;
1066 BT_ASSERT(ret_len
== len
);
1079 * Assign the fields from a lttng_viewer_index to a packet_index.
1082 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
1083 struct packet_index
*pindex
)
1088 pindex
->offset
= be64toh(lindex
->offset
);
1089 pindex
->packet_size
= be64toh(lindex
->packet_size
);
1090 pindex
->content_size
= be64toh(lindex
->content_size
);
1091 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
1092 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
1093 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
1097 enum lttng_live_iterator_status
lttng_live_get_next_index(
1098 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1099 struct lttng_live_stream_iterator
*stream
,
1100 struct packet_index
*index
)
1102 struct lttng_viewer_cmd cmd
;
1103 struct lttng_viewer_get_next_index rq
;
1104 struct lttng_viewer_index rp
;
1105 enum lttng_live_iterator_status retstatus
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1106 struct live_viewer_connection
*viewer_connection
=
1107 lttng_live_msg_iter
->viewer_connection
;
1108 struct lttng_live_trace
*trace
= stream
->trace
;
1109 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1110 char cmd_buf
[cmd_buf_len
];
1111 uint32_t flags
, status
;
1114 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
1115 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1116 cmd
.cmd_version
= htobe32(0);
1119 memset(&rq
, 0, sizeof(rq
));
1120 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1123 * Merge the cmd and connection request to prevent a write-write
1124 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1125 * second write to be performed quickly in presence of Nagle's algorithm.
1127 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1128 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1129 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1130 if (ret_len
== BT_SOCKET_ERROR
) {
1131 BT_COMP_LOGE("Error sending get_next_index request: %s",
1132 bt_socket_errormsg());
1136 BT_ASSERT(ret_len
== cmd_buf_len
);
1137 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1139 BT_COMP_LOGI("Remote side has closed connection");
1142 if (ret_len
== BT_SOCKET_ERROR
) {
1143 BT_COMP_LOGE("Error receiving get_next_index response: %s",
1144 bt_socket_errormsg());
1147 BT_ASSERT(ret_len
== sizeof(rp
));
1149 flags
= be32toh(rp
.flags
);
1150 status
= be32toh(rp
.status
);
1153 case LTTNG_VIEWER_INDEX_INACTIVE
:
1155 uint64_t ctf_stream_class_id
;
1157 BT_COMP_LOGD("get_next_index: inactive");
1158 memset(index
, 0, sizeof(struct packet_index
));
1159 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
1160 stream
->current_inactivity_ts
= index
->ts_cycles
.timestamp_end
;
1161 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1162 if (stream
->ctf_stream_class_id
!= -1ULL) {
1163 BT_ASSERT(stream
->ctf_stream_class_id
==
1164 ctf_stream_class_id
);
1166 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1168 stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT
;
1171 case LTTNG_VIEWER_INDEX_OK
:
1173 uint64_t ctf_stream_class_id
;
1175 BT_COMP_LOGD("get_next_index: OK");
1176 lttng_index_to_packet_index(&rp
, index
);
1177 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1178 if (stream
->ctf_stream_class_id
!= -1ULL) {
1179 BT_ASSERT(stream
->ctf_stream_class_id
==
1180 ctf_stream_class_id
);
1182 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1185 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_DATA
;
1187 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1188 BT_COMP_LOGD("get_next_index: new metadata needed");
1189 trace
->new_metadata_needed
= true;
1191 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1192 BT_COMP_LOGD("get_next_index: new streams needed");
1193 lttng_live_need_new_streams(lttng_live_msg_iter
);
1197 case LTTNG_VIEWER_INDEX_RETRY
:
1198 BT_COMP_LOGD("get_next_index: retry");
1199 memset(index
, 0, sizeof(struct packet_index
));
1200 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1201 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1203 case LTTNG_VIEWER_INDEX_HUP
:
1204 BT_COMP_LOGD("get_next_index: stream hung up");
1205 memset(index
, 0, sizeof(struct packet_index
));
1206 index
->offset
= EOF
;
1207 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1208 stream
->state
= LTTNG_LIVE_STREAM_EOF
;
1209 stream
->has_stream_hung_up
= true;
1211 case LTTNG_VIEWER_INDEX_ERR
:
1212 BT_COMP_LOGE("get_next_index: error");
1213 memset(index
, 0, sizeof(struct packet_index
));
1214 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1217 BT_COMP_LOGE("get_next_index: unknown value");
1218 memset(index
, 0, sizeof(struct packet_index
));
1219 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1226 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
1227 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1229 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1235 enum bt_msg_iter_medium_status
lttng_live_get_stream_bytes(
1236 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1237 struct lttng_live_stream_iterator
*stream
, uint8_t *buf
,
1238 uint64_t offset
, uint64_t req_len
, uint64_t *recv_len
)
1240 enum bt_msg_iter_medium_status retstatus
= BT_MSG_ITER_MEDIUM_STATUS_OK
;
1241 struct lttng_viewer_trace_packet rp
;
1242 struct lttng_viewer_cmd cmd
;
1243 struct lttng_viewer_get_packet rq
;
1244 struct live_viewer_connection
*viewer_connection
=
1245 lttng_live_msg_iter
->viewer_connection
;
1246 struct lttng_live_trace
*trace
= stream
->trace
;
1247 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1248 char cmd_buf
[cmd_buf_len
];
1249 uint32_t flags
, status
;
1252 BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64
", req_len=%" PRIu64
,
1254 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
1255 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1256 cmd
.cmd_version
= htobe32(0);
1258 memset(&rq
, 0, sizeof(rq
));
1259 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1260 rq
.offset
= htobe64(offset
);
1261 rq
.len
= htobe32(req_len
);
1264 * Merge the cmd and connection request to prevent a write-write
1265 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1266 * second write to be performed quickly in presence of Nagle's algorithm.
1268 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1269 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1270 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1271 if (ret_len
== BT_SOCKET_ERROR
) {
1272 BT_COMP_LOGE("Error sending get_data request: %s", bt_socket_errormsg());
1276 BT_ASSERT(ret_len
== cmd_buf_len
);
1277 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1279 BT_COMP_LOGI("Remote side has closed connection");
1282 if (ret_len
== BT_SOCKET_ERROR
) {
1283 BT_COMP_LOGE("Error receiving get_data response: %s", bt_socket_errormsg());
1286 if (ret_len
!= sizeof(rp
)) {
1287 BT_COMP_LOGE("get_data_packet: expected %zu"
1288 ", received %zd", sizeof(rp
),
1293 flags
= be32toh(rp
.flags
);
1294 status
= be32toh(rp
.status
);
1297 case LTTNG_VIEWER_GET_PACKET_OK
:
1298 req_len
= be32toh(rp
.len
);
1299 BT_COMP_LOGD("get_data_packet: Ok, packet size : %" PRIu64
"", req_len
);
1301 case LTTNG_VIEWER_GET_PACKET_RETRY
:
1302 /* Unimplemented by relay daemon */
1303 BT_COMP_LOGD("get_data_packet: retry");
1304 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1306 case LTTNG_VIEWER_GET_PACKET_ERR
:
1307 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1308 BT_COMP_LOGD("get_data_packet: new metadata needed, try again later");
1309 trace
->new_metadata_needed
= true;
1311 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1312 BT_COMP_LOGD("get_data_packet: new streams needed, try again later");
1313 lttng_live_need_new_streams(lttng_live_msg_iter
);
1315 if (flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
1316 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
1317 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1320 BT_COMP_LOGE("get_data_packet: error");
1322 case LTTNG_VIEWER_GET_PACKET_EOF
:
1323 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_EOF
;
1326 BT_COMP_LOGE("get_data_packet: unknown");
1334 ret_len
= lttng_live_recv(viewer_connection
, buf
, req_len
);
1336 BT_COMP_LOGI("Remote side has closed connection");
1339 if (ret_len
== BT_SOCKET_ERROR
) {
1340 BT_COMP_LOGE("Error receiving trace packet: %s", bt_socket_errormsg());
1343 BT_ASSERT(ret_len
== req_len
);
1344 *recv_len
= ret_len
;
1349 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
1350 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1352 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
1358 * Request new streams for a session.
1361 enum lttng_live_iterator_status
lttng_live_get_new_streams(
1362 struct lttng_live_session
*session
)
1364 enum lttng_live_iterator_status status
=
1365 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1366 struct lttng_viewer_cmd cmd
;
1367 struct lttng_viewer_new_streams_request rq
;
1368 struct lttng_viewer_new_streams_response rp
;
1369 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1370 session
->lttng_live_msg_iter
;
1371 struct live_viewer_connection
*viewer_connection
=
1372 lttng_live_msg_iter
->viewer_connection
;
1373 uint32_t streams_count
;
1374 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1375 char cmd_buf
[cmd_buf_len
];
1378 if (!session
->new_streams_needed
) {
1379 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
1382 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1383 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1384 cmd
.cmd_version
= htobe32(0);
1386 memset(&rq
, 0, sizeof(rq
));
1387 rq
.session_id
= htobe64(session
->id
);
1390 * Merge the cmd and connection request to prevent a write-write
1391 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1392 * second write to be performed quickly in presence of Nagle's algorithm.
1394 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1395 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1396 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1397 if (ret_len
== BT_SOCKET_ERROR
) {
1398 BT_COMP_LOGE("Error sending get_new_streams request: %s",
1399 bt_socket_errormsg());
1403 BT_ASSERT(ret_len
== cmd_buf_len
);
1404 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1406 BT_COMP_LOGI("Remote side has closed connection");
1409 if (ret_len
== BT_SOCKET_ERROR
) {
1410 BT_COMP_LOGE("Error receiving get_new_streams response");
1413 BT_ASSERT(ret_len
== sizeof(rp
));
1415 streams_count
= be32toh(rp
.streams_count
);
1417 switch(be32toh(rp
.status
)) {
1418 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1419 session
->new_streams_needed
= false;
1421 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1422 session
->new_streams_needed
= false;
1424 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1425 session
->new_streams_needed
= false;
1426 session
->closed
= true;
1427 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1429 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1430 BT_COMP_LOGE("get_new_streams error");
1433 BT_COMP_LOGE("Unknown return code %u", be32toh(rp
.status
));
1437 if (receive_streams(session
, streams_count
)) {
1444 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
1445 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1447 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1453 struct live_viewer_connection
*live_viewer_connection_create(
1454 const char *url
, bool in_query
,
1455 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1456 bt_logging_level log_level
)
1458 struct live_viewer_connection
*viewer_connection
;
1460 viewer_connection
= g_new0(struct live_viewer_connection
, 1);
1462 if (bt_socket_init(log_level
) != 0) {
1466 viewer_connection
->log_level
= log_level
;
1468 if (lttng_live_msg_iter
) {
1469 viewer_connection
->self_comp
= lttng_live_msg_iter
->self_comp
;
1472 bt_object_init_shared(&viewer_connection
->obj
, connection_release
);
1473 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
1474 viewer_connection
->port
= -1;
1475 viewer_connection
->in_query
= in_query
;
1476 viewer_connection
->lttng_live_msg_iter
= lttng_live_msg_iter
;
1477 viewer_connection
->url
= g_string_new(url
);
1478 if (!viewer_connection
->url
) {
1482 BT_COMP_LOGI("Establishing connection to url \"%s\"...", url
);
1483 if (lttng_live_connect_viewer(viewer_connection
)) {
1486 BT_COMP_LOGI("Connection to url \"%s\" is established", url
);
1487 return viewer_connection
;
1490 BT_COMP_LOGW("Failure to establish connection to url \"%s\"", url
);
1492 if (viewer_connection
) {
1493 live_viewer_connection_destroy(viewer_connection
);
1500 void live_viewer_connection_destroy(
1501 struct live_viewer_connection
*viewer_connection
)
1503 BT_COMP_LOGI("Closing connection to url \"%s\"", viewer_connection
->url
->str
);
1504 lttng_live_disconnect_viewer(viewer_connection
);
1505 if (viewer_connection
->url
) {
1506 g_string_free(viewer_connection
->url
, true);
1508 if (viewer_connection
->relay_hostname
) {
1509 g_string_free(viewer_connection
->relay_hostname
, true);
1511 if (viewer_connection
->target_hostname
) {
1512 g_string_free(viewer_connection
->target_hostname
, true);
1514 if (viewer_connection
->session_name
) {
1515 g_string_free(viewer_connection
->session_name
, true);
1517 g_free(viewer_connection
);