2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * 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 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <netinet/in.h>
38 #include <babeltrace/ctf/ctf-index.h>
40 #include <babeltrace/babeltrace.h>
41 #include <babeltrace/ctf/events.h>
42 #include <babeltrace/ctf/callbacks.h>
43 #include <babeltrace/ctf/iterator.h>
45 /* for packet_index */
46 #include <babeltrace/ctf/types.h>
48 #include <babeltrace/ctf/metadata.h>
49 #include <babeltrace/ctf-text/types.h>
50 #include <babeltrace/ctf/events-internal.h>
51 #include <babeltrace/endian.h>
52 #include <babeltrace/compat/memstream.h>
54 #include <formats/ctf/events-private.h>
57 #include "network-live.h"
60 #include "lttng-live-comm.h"
61 #include "lttng-viewer-abi.h"
63 #define ACTIVE_POLL_DELAY 100 /* ms */
66 * Memory allocation zeroed
68 #define zmalloc(x) calloc(1, x)
71 #define max_t(type, a, b) \
72 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
75 static void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
,
76 size_t index
, int whence
);
77 static void add_traces(gpointer key
, gpointer value
, gpointer user_data
);
78 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
);
79 static int get_new_metadata(struct lttng_live_ctx
*ctx
,
80 struct lttng_live_viewer_stream
*viewer_stream
,
84 ssize_t
lttng_live_recv(int fd
, void *buf
, size_t len
)
87 size_t copied
= 0, to_copy
= len
;
90 ret
= recv(fd
, buf
+ copied
, to_copy
, 0);
92 assert(ret
<= to_copy
);
96 } while ((ret
> 0 && to_copy
> 0)
97 || (ret
< 0 && errno
== EINTR
));
100 /* ret = 0 means orderly shutdown, ret < 0 is error. */
105 ssize_t
lttng_live_send(int fd
, const void *buf
, size_t len
)
110 ret
= send(fd
, buf
, len
, MSG_NOSIGNAL
);
111 } while (ret
< 0 && errno
== EINTR
);
115 int lttng_live_connect_viewer(struct lttng_live_ctx
*ctx
)
117 struct hostent
*host
;
118 struct sockaddr_in server_addr
;
121 if (lttng_live_should_quit()) {
126 host
= gethostbyname(ctx
->relay_hostname
);
128 fprintf(stderr
, "[error] Cannot lookup hostname %s\n",
129 ctx
->relay_hostname
);
133 if ((ctx
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
138 server_addr
.sin_family
= AF_INET
;
139 server_addr
.sin_port
= htons(ctx
->port
);
140 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
141 bzero(&(server_addr
.sin_zero
), 8);
143 if (connect(ctx
->control_sock
, (struct sockaddr
*) &server_addr
,
144 sizeof(struct sockaddr
)) == -1) {
155 fprintf(stderr
, "[error] Connection failed\n");
159 int lttng_live_establish_connection(struct lttng_live_ctx
*ctx
)
161 struct lttng_viewer_cmd cmd
;
162 struct lttng_viewer_connect connect
;
166 if (lttng_live_should_quit()) {
171 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
172 cmd
.data_size
= sizeof(connect
);
175 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
176 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
177 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
178 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
180 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
182 perror("[error] Error sending cmd");
185 assert(ret_len
== sizeof(cmd
));
187 ret_len
= lttng_live_send(ctx
->control_sock
, &connect
, sizeof(connect
));
189 perror("[error] Error sending version");
192 assert(ret_len
== sizeof(connect
));
194 ret_len
= lttng_live_recv(ctx
->control_sock
, &connect
, sizeof(connect
));
196 fprintf(stderr
, "[error] Remote side has closed connection\n");
200 perror("[error] Error receiving version");
203 assert(ret_len
== sizeof(connect
));
205 printf_verbose("Received viewer session ID : %" PRIu64
"\n",
206 be64toh(connect
.viewer_session_id
));
207 printf_verbose("Relayd version : %u.%u\n", be32toh(connect
.major
),
208 be32toh(connect
.minor
));
210 if (LTTNG_LIVE_MAJOR
!= be32toh(connect
.major
)) {
211 fprintf(stderr
, "[error] Incompatible lttng-relayd protocol\n");
214 /* Use the smallest protocol version implemented. */
215 if (LTTNG_LIVE_MINOR
> be32toh(connect
.minor
)) {
216 ctx
->minor
= be32toh(connect
.minor
);
218 ctx
->minor
= LTTNG_LIVE_MINOR
;
220 ctx
->major
= LTTNG_LIVE_MAJOR
;
226 fprintf(stderr
, "[error] Unable to establish connection\n");
231 void free_session_list(GPtrArray
*session_list
)
234 struct lttng_live_relay_session
*relay_session
;
236 for (i
= 0; i
< session_list
->len
; i
++) {
237 relay_session
= g_ptr_array_index(session_list
, i
);
238 free(relay_session
->name
);
239 free(relay_session
->hostname
);
241 g_ptr_array_free(session_list
, TRUE
);
245 void print_session_list(GPtrArray
*session_list
, const char *path
)
248 struct lttng_live_relay_session
*relay_session
;
250 for (i
= 0; i
< session_list
->len
; i
++) {
251 relay_session
= g_ptr_array_index(session_list
, i
);
252 fprintf(stdout
, "%s/host/%s/%s (timer = %u, "
253 "%u stream(s), %u client(s) connected)\n",
254 path
, relay_session
->hostname
,
255 relay_session
->name
, relay_session
->timer
,
256 relay_session
->streams
, relay_session
->clients
);
261 void update_session_list(GPtrArray
*session_list
, char *hostname
,
262 char *session_name
, uint32_t streams
, uint32_t clients
,
266 struct lttng_live_relay_session
*relay_session
;
268 for (i
= 0; i
< session_list
->len
; i
++) {
269 relay_session
= g_ptr_array_index(session_list
, i
);
270 if ((strncmp(relay_session
->hostname
, hostname
, NAME_MAX
) == 0) &&
271 strncmp(relay_session
->name
,
272 session_name
, NAME_MAX
) == 0) {
273 relay_session
->streams
+= streams
;
274 if (relay_session
->clients
< clients
)
275 relay_session
->clients
= clients
;
283 relay_session
= g_new0(struct lttng_live_relay_session
, 1);
284 relay_session
->hostname
= strndup(hostname
, NAME_MAX
);
285 relay_session
->name
= strndup(session_name
, NAME_MAX
);
286 relay_session
->clients
= clients
;
287 relay_session
->streams
= streams
;
288 relay_session
->timer
= timer
;
289 g_ptr_array_add(session_list
, relay_session
);
292 int lttng_live_list_sessions(struct lttng_live_ctx
*ctx
, const char *path
)
294 struct lttng_viewer_cmd cmd
;
295 struct lttng_viewer_list_sessions list
;
296 struct lttng_viewer_session lsession
;
297 int i
, ret
, sessions_count
, print_list
= 0;
300 GPtrArray
*session_list
= NULL
;
302 if (lttng_live_should_quit()) {
307 if (strlen(ctx
->session_name
) == 0) {
309 session_list
= g_ptr_array_new();
312 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
316 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
318 perror("[error] Error sending cmd");
321 assert(ret_len
== sizeof(cmd
));
323 ret_len
= lttng_live_recv(ctx
->control_sock
, &list
, sizeof(list
));
325 fprintf(stderr
, "[error] Remote side has closed connection\n");
329 perror("[error] Error receiving session list");
332 assert(ret_len
== sizeof(list
));
334 sessions_count
= be32toh(list
.sessions_count
);
335 for (i
= 0; i
< sessions_count
; i
++) {
336 ret_len
= lttng_live_recv(ctx
->control_sock
, &lsession
, sizeof(lsession
));
338 fprintf(stderr
, "[error] Remote side has closed connection\n");
342 perror("[error] Error receiving session");
345 assert(ret_len
== sizeof(lsession
));
346 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
347 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
348 session_id
= be64toh(lsession
.id
);
351 update_session_list(session_list
,
353 lsession
.session_name
,
354 be32toh(lsession
.streams
),
355 be32toh(lsession
.clients
),
356 be32toh(lsession
.live_timer
));
358 if ((strncmp(lsession
.session_name
, ctx
->session_name
,
359 NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
360 ctx
->traced_hostname
, NAME_MAX
) == 0)) {
361 printf_verbose("Reading from session %" PRIu64
"\n",
363 g_array_append_val(ctx
->session_ids
,
370 print_session_list(session_list
, path
);
371 free_session_list(session_list
);
378 fprintf(stderr
, "[error] Unable to list sessions\n");
382 int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream
*stream
,
383 uint64_t ctf_trace_id
)
385 struct lttng_live_ctf_trace
*trace
;
388 trace
= g_hash_table_lookup(stream
->session
->ctf_traces
,
389 (gpointer
) ctf_trace_id
);
391 trace
= g_new0(struct lttng_live_ctf_trace
, 1);
392 trace
->ctf_trace_id
= ctf_trace_id
;
393 trace
->streams
= g_ptr_array_new();
394 g_hash_table_insert(stream
->session
->ctf_traces
,
395 (gpointer
) ctf_trace_id
,
398 if (stream
->metadata_flag
)
399 trace
->metadata_stream
= stream
;
401 stream
->ctf_trace
= trace
;
402 g_ptr_array_add(trace
->streams
, stream
);
408 int open_metadata_fp_write(struct lttng_live_viewer_stream
*stream
,
409 char **metadata_buf
, size_t *size
)
413 stream
->metadata_fp_write
=
414 babeltrace_open_memstream(metadata_buf
, size
);
415 if (!stream
->metadata_fp_write
) {
416 perror("Metadata open_memstream");
423 int lttng_live_attach_session(struct lttng_live_ctx
*ctx
, uint64_t id
)
425 struct lttng_viewer_cmd cmd
;
426 struct lttng_viewer_attach_session_request rq
;
427 struct lttng_viewer_attach_session_response rp
;
428 struct lttng_viewer_stream stream
;
432 if (lttng_live_should_quit()) {
437 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
438 cmd
.data_size
= sizeof(rq
);
441 memset(&rq
, 0, sizeof(rq
));
442 rq
.session_id
= htobe64(id
);
443 // TODO: add cmd line parameter to select seek beginning
444 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
445 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
447 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
449 perror("[error] Error sending cmd");
452 assert(ret_len
== sizeof(cmd
));
454 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
456 perror("[error] Error sending attach request");
459 assert(ret_len
== sizeof(rq
));
461 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
463 fprintf(stderr
, "[error] Remote side has closed connection\n");
467 perror("[error] Error receiving attach response");
470 assert(ret_len
== sizeof(rp
));
472 switch(be32toh(rp
.status
)) {
473 case LTTNG_VIEWER_ATTACH_OK
:
475 case LTTNG_VIEWER_ATTACH_UNK
:
476 ret
= -LTTNG_VIEWER_ATTACH_UNK
;
478 case LTTNG_VIEWER_ATTACH_ALREADY
:
479 fprintf(stderr
, "[error] There is already a viewer attached to this session\n");
481 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
482 fprintf(stderr
, "[error] Not a live session\n");
484 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
485 fprintf(stderr
, "[error] Wrong seek parameter\n");
488 fprintf(stderr
, "[error] Unknown attach return code %u\n",
492 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
496 ctx
->session
->stream_count
+= be32toh(rp
.streams_count
);
498 * When the session is created but not started, we do an active wait
499 * until it starts. It allows the viewer to start processing the trace
500 * as soon as the session starts.
502 if (ctx
->session
->stream_count
== 0) {
506 printf_verbose("Waiting for %" PRIu64
" streams:\n",
507 ctx
->session
->stream_count
);
508 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
509 ctx
->session
->stream_count
);
510 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
511 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
, sizeof(stream
));
513 fprintf(stderr
, "[error] Remote side has closed connection\n");
517 perror("[error] Error receiving stream");
520 assert(ret_len
== sizeof(stream
));
521 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
522 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
524 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
525 be64toh(stream
.id
), stream
.path_name
,
526 stream
.channel_name
);
527 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
528 ctx
->session
->streams
[i
].session
= ctx
->session
;
530 ctx
->session
->streams
[i
].mmap_size
= 0;
531 ctx
->session
->streams
[i
].ctf_stream_id
= -1ULL;
533 if (be32toh(stream
.metadata_flag
)) {
534 ctx
->session
->streams
[i
].metadata_flag
= 1;
536 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
537 be64toh(stream
.ctf_trace_id
));
552 * Ask the relay for new streams.
554 * Returns the number of new streams received or a negative value on error.
557 int ask_new_streams(struct lttng_live_ctx
*ctx
)
559 int i
, ret
= 0, nb_streams
= 0;
563 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
564 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
565 ret
= lttng_live_get_new_streams(ctx
, id
);
566 printf_verbose("Asking for new streams returns %d\n", ret
);
568 if (lttng_live_should_quit()) {
571 if (ret
== -LTTNG_VIEWER_NEW_STREAMS_HUP
) {
572 printf_verbose("Session %" PRIu64
" closed\n",
575 * The streams have already been closed during
576 * the reading, so we only need to get rid of
577 * the trace in our internal table of sessions.
579 g_array_remove_index(ctx
->session_ids
, i
);
581 * We can't continue iterating on the g_array
582 * after a remove, we have to start again.
600 int append_metadata(struct lttng_live_ctx
*ctx
,
601 struct lttng_live_viewer_stream
*viewer_stream
)
604 struct lttng_live_viewer_stream
*metadata
;
605 char *metadata_buf
= NULL
;
607 printf_verbose("get_next_index: new metadata needed\n");
608 ret
= get_new_metadata(ctx
, viewer_stream
, &metadata_buf
);
614 metadata
= viewer_stream
->ctf_trace
->metadata_stream
;
615 metadata
->ctf_trace
->metadata_fp
=
616 babeltrace_fmemopen(metadata_buf
,
617 metadata
->metadata_len
, "rb");
618 if (!metadata
->ctf_trace
->metadata_fp
) {
619 perror("Metadata fmemopen");
624 ret
= ctf_append_trace_metadata(
625 viewer_stream
->ctf_trace
->handle
->td
,
626 metadata
->ctf_trace
->metadata_fp
);
627 /* We accept empty metadata packets */
628 if (ret
!= 0 && ret
!= -ENOENT
) {
629 fprintf(stderr
, "[error] Appending metadata\n");
639 int get_data_packet(struct lttng_live_ctx
*ctx
,
640 struct ctf_stream_pos
*pos
,
641 struct lttng_live_viewer_stream
*stream
, uint64_t offset
,
644 struct lttng_viewer_cmd cmd
;
645 struct lttng_viewer_get_packet rq
;
646 struct lttng_viewer_trace_packet rp
;
651 if (lttng_live_should_quit()) {
655 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
656 cmd
.data_size
= sizeof(rq
);
659 memset(&rq
, 0, sizeof(rq
));
660 rq
.stream_id
= htobe64(stream
->id
);
661 rq
.offset
= htobe64(offset
);
662 rq
.len
= htobe32(len
);
664 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
666 perror("[error] Error sending cmd");
669 assert(ret_len
== sizeof(cmd
));
671 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
673 perror("[error] Error sending get_data_packet request");
676 assert(ret_len
== sizeof(rq
));
678 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
680 fprintf(stderr
, "[error] Remote side has closed connection\n");
684 perror("[error] Error receiving data response");
687 if (ret_len
!= sizeof(rp
)) {
688 fprintf(stderr
, "[error] get_data_packet: expected %" PRId64
689 ", received %" PRId64
"\n", sizeof(rp
),
694 rp
.flags
= be32toh(rp
.flags
);
696 switch (be32toh(rp
.status
)) {
697 case LTTNG_VIEWER_GET_PACKET_OK
:
698 len
= be32toh(rp
.len
);
699 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
702 case LTTNG_VIEWER_GET_PACKET_RETRY
:
703 /* Unimplemented by relay daemon */
704 printf_verbose("get_data_packet: retry\n");
706 case LTTNG_VIEWER_GET_PACKET_ERR
:
707 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
708 printf_verbose("get_data_packet: new metadata needed\n");
709 ret
= append_metadata(ctx
, stream
);
713 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
714 printf_verbose("get_data_packet: new streams needed\n");
715 ret
= ask_new_streams(ctx
);
719 g_hash_table_foreach(ctx
->session
->ctf_traces
,
720 add_traces
, ctx
->bt_ctx
);
722 if (rp
.flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
723 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
726 fprintf(stderr
, "[error] get_data_packet: error\n");
728 case LTTNG_VIEWER_GET_PACKET_EOF
:
732 printf_verbose("get_data_packet: unknown\n");
740 if (len
> stream
->mmap_size
) {
743 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
746 ret
= munmap_align(pos
->base_mma
);
748 perror("[error] Unable to unmap old base");
751 pos
->base_mma
= NULL
;
753 pos
->base_mma
= mmap_align(new_size
,
754 PROT_READ
| PROT_WRITE
,
755 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
756 if (pos
->base_mma
== MAP_FAILED
) {
757 perror("[error] mmap error");
758 pos
->base_mma
= NULL
;
762 stream
->mmap_size
= new_size
;
763 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
767 ret_len
= lttng_live_recv(ctx
->control_sock
,
768 mmap_align_addr(pos
->base_mma
), len
);
770 fprintf(stderr
, "[error] Remote side has closed connection\n");
774 perror("[error] Error receiving trace packet");
777 assert(ret_len
== len
);
787 int get_one_metadata_packet(struct lttng_live_ctx
*ctx
,
788 struct lttng_live_viewer_stream
*metadata_stream
)
792 struct lttng_viewer_cmd cmd
;
793 struct lttng_viewer_get_metadata rq
;
794 struct lttng_viewer_metadata_packet rp
;
798 if (lttng_live_should_quit()) {
803 rq
.stream_id
= htobe64(metadata_stream
->id
);
804 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
805 cmd
.data_size
= sizeof(rq
);
808 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
810 perror("[error] Error sending cmd");
813 assert(ret_len
== sizeof(cmd
));
815 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
817 perror("[error] Error sending get_metadata request");
820 assert(ret_len
== sizeof(rq
));
822 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
824 fprintf(stderr
, "[error] Remote side has closed connection\n");
828 perror("[error] Error receiving metadata response");
831 assert(ret_len
== sizeof(rp
));
833 switch (be32toh(rp
.status
)) {
834 case LTTNG_VIEWER_METADATA_OK
:
835 printf_verbose("get_metadata : OK\n");
837 case LTTNG_VIEWER_NO_NEW_METADATA
:
838 printf_verbose("get_metadata : NO NEW\n");
841 case LTTNG_VIEWER_METADATA_ERR
:
842 printf_verbose("get_metadata : ERR\n");
845 printf_verbose("get_metadata : UNKNOWN\n");
849 len
= be64toh(rp
.len
);
850 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
857 perror("relay data zmalloc");
860 ret_len
= lttng_live_recv(ctx
->control_sock
, data
, len
);
862 fprintf(stderr
, "[error] Remote side has closed connection\n");
863 goto error_free_data
;
866 perror("[error] Error receiving trace packet");
867 goto error_free_data
;
869 assert(ret_len
== len
);
872 ret_len
= fwrite(data
, 1, len
,
873 metadata_stream
->metadata_fp_write
);
874 } while (ret_len
< 0 && errno
== EINTR
);
876 fprintf(stderr
, "[error] Writing in the metadata fp\n");
877 goto error_free_data
;
879 assert(ret_len
== len
);
880 metadata_stream
->metadata_len
+= len
;
893 * Return 0 on success, a negative value on error.
896 int get_new_metadata(struct lttng_live_ctx
*ctx
,
897 struct lttng_live_viewer_stream
*viewer_stream
,
901 struct lttng_live_viewer_stream
*metadata_stream
;
902 size_t size
, len_read
= 0;
904 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
905 if (!metadata_stream
) {
906 fprintf(stderr
, "[error] No metadata stream\n");
910 metadata_stream
->metadata_len
= 0;
911 ret
= open_metadata_fp_write(metadata_stream
, metadata_buf
, &size
);
918 * get_one_metadata_packet returns the number of bytes
919 * received, 0 when we have received everything, a
920 * negative value on error.
922 ret
= get_one_metadata_packet(ctx
, metadata_stream
);
927 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
929 } while (ret
> 0 || !len_read
);
931 if (fclose(metadata_stream
->metadata_fp_write
))
933 metadata_stream
->metadata_fp_write
= NULL
;
940 * Assign the fields from a lttng_viewer_index to a packet_index.
943 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
944 struct packet_index
*pindex
)
949 pindex
->offset
= be64toh(lindex
->offset
);
950 pindex
->packet_size
= be64toh(lindex
->packet_size
);
951 pindex
->content_size
= be64toh(lindex
->content_size
);
952 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
953 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
954 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
958 * Get one index for a stream.
960 * Returns 0 on success or a negative value on error.
963 int get_next_index(struct lttng_live_ctx
*ctx
,
964 struct lttng_live_viewer_stream
*viewer_stream
,
965 struct packet_index
*index
, uint64_t *stream_id
)
967 struct lttng_viewer_cmd cmd
;
968 struct lttng_viewer_get_next_index rq
;
971 struct lttng_viewer_index
*rp
= &viewer_stream
->current_index
;
973 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
974 cmd
.data_size
= sizeof(rq
);
977 memset(&rq
, 0, sizeof(rq
));
978 rq
.stream_id
= htobe64(viewer_stream
->id
);
981 if (lttng_live_should_quit()) {
985 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
987 perror("[error] Error sending cmd");
990 assert(ret_len
== sizeof(cmd
));
992 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
994 perror("[error] Error sending get_next_index request");
997 assert(ret_len
== sizeof(rq
));
999 ret_len
= lttng_live_recv(ctx
->control_sock
, rp
, sizeof(*rp
));
1001 fprintf(stderr
, "[error] Remote side has closed connection\n");
1005 perror("[error] Error receiving index response");
1008 assert(ret_len
== sizeof(*rp
));
1010 rp
->flags
= be32toh(rp
->flags
);
1012 switch (be32toh(rp
->status
)) {
1013 case LTTNG_VIEWER_INDEX_INACTIVE
:
1014 printf_verbose("get_next_index: inactive\n");
1015 memset(index
, 0, sizeof(struct packet_index
));
1016 index
->ts_cycles
.timestamp_end
= be64toh(rp
->timestamp_end
);
1017 *stream_id
= be64toh(rp
->stream_id
);
1019 case LTTNG_VIEWER_INDEX_OK
:
1020 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
1021 rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
1022 lttng_index_to_packet_index(rp
, index
);
1023 *stream_id
= be64toh(rp
->stream_id
);
1024 viewer_stream
->data_pending
= 1;
1026 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1027 ret
= append_metadata(ctx
, viewer_stream
);
1031 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1032 printf_verbose("get_next_index: need new streams\n");
1033 ret
= ask_new_streams(ctx
);
1037 g_hash_table_foreach(ctx
->session
->ctf_traces
,
1038 add_traces
, ctx
->bt_ctx
);
1041 case LTTNG_VIEWER_INDEX_RETRY
:
1042 printf_verbose("get_next_index: retry\n");
1043 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1045 case LTTNG_VIEWER_INDEX_HUP
:
1046 printf_verbose("get_next_index: stream hung up\n");
1047 viewer_stream
->id
= -1ULL;
1048 index
->offset
= EOF
;
1049 ctx
->session
->stream_count
--;
1051 case LTTNG_VIEWER_INDEX_ERR
:
1052 fprintf(stderr
, "[error] get_next_index: error\n");
1055 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
1067 void read_packet_header(struct ctf_stream_pos
*pos
,
1068 struct ctf_file_stream
*file_stream
)
1072 /* update trace_packet_header and stream_packet_context */
1073 if (!(pos
->prot
& PROT_WRITE
) &&
1074 file_stream
->parent
.trace_packet_header
) {
1075 /* Read packet header */
1076 ret
= generic_rw(&pos
->parent
,
1077 &file_stream
->parent
.trace_packet_header
->p
);
1080 fprintf(stderr
, "[error] trace packet "
1081 "header read failed\n");
1085 if (!(pos
->prot
& PROT_WRITE
) &&
1086 file_stream
->parent
.stream_packet_context
) {
1087 /* Read packet context */
1088 ret
= generic_rw(&pos
->parent
,
1089 &file_stream
->parent
.stream_packet_context
->p
);
1092 fprintf(stderr
, "[error] stream packet "
1093 "context read failed\n");
1097 pos
->data_offset
= pos
->offset
;
1104 * Handle the seek parameters.
1105 * Returns 0 if the packet_seek can continue, a positive value to
1106 * cleanly exit the packet_seek, a negative value on error.
1109 int handle_seek_position(size_t index
, int whence
,
1110 struct lttng_live_viewer_stream
*viewer_stream
,
1111 struct ctf_stream_pos
*pos
,
1112 struct ctf_file_stream
*file_stream
)
1122 * We only allow to seek to 0.
1125 fprintf(stderr
, "[error] Arbitrary seek in lttng-live "
1126 "trace not supported\n");
1136 fprintf(stderr
, "[error] Invalid seek parameter\n");
1145 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
1148 struct ctf_stream_pos
*pos
;
1149 struct ctf_file_stream
*file_stream
;
1150 struct packet_index
*prev_index
= NULL
, *cur_index
;
1151 struct lttng_live_viewer_stream
*viewer_stream
;
1152 struct lttng_live_session
*session
;
1153 uint64_t stream_id
= -1ULL;
1156 pos
= ctf_pos(stream_pos
);
1157 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
1158 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
1159 session
= viewer_stream
->session
;
1161 ret
= handle_seek_position(index
, whence
, viewer_stream
, pos
,
1168 switch (pos
->packet_index
->len
) {
1170 g_array_set_size(pos
->packet_index
, 1);
1171 cur_index
= &g_array_index(pos
->packet_index
,
1172 struct packet_index
, 0);
1175 g_array_set_size(pos
->packet_index
, 2);
1176 prev_index
= &g_array_index(pos
->packet_index
,
1177 struct packet_index
, 0);
1178 cur_index
= &g_array_index(pos
->packet_index
,
1179 struct packet_index
, 1);
1182 g_array_index(pos
->packet_index
,
1183 struct packet_index
, 0) =
1184 g_array_index(pos
->packet_index
,
1185 struct packet_index
, 1);
1186 prev_index
= &g_array_index(pos
->packet_index
,
1187 struct packet_index
, 0);
1188 cur_index
= &g_array_index(pos
->packet_index
,
1189 struct packet_index
, 1);
1196 if (viewer_stream
->data_pending
) {
1197 lttng_index_to_packet_index(&viewer_stream
->current_index
, cur_index
);
1199 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
1200 ret
= get_next_index(session
->ctx
, viewer_stream
, cur_index
, &stream_id
);
1203 if (!lttng_live_should_quit()) {
1204 fprintf(stderr
, "[error] get_next_index failed\n");
1208 printf_verbose("Index received : packet_size : %" PRIu64
1209 ", offset %" PRIu64
", content_size %" PRIu64
1210 ", timestamp_end : %" PRIu64
"\n",
1211 cur_index
->packet_size
, cur_index
->offset
,
1212 cur_index
->content_size
,
1213 cur_index
->ts_cycles
.timestamp_end
);
1218 * On the first time we receive an index, the stream_id needs to
1219 * be set for the stream in order to use it, we don't want any
1220 * data at this stage.
1222 if (file_stream
->parent
.stream_id
== -1ULL) {
1224 * Warning: with lttng-tools < 2.4.2, the beacon does not
1225 * contain the real stream ID, it is memset to 0, so this
1226 * might create a problem when a session has multiple
1227 * channels. We can't detect it at this stage, lttng-tools
1228 * has to be upgraded to fix this problem.
1230 printf_verbose("Assigning stream_id %" PRIu64
"\n",
1232 file_stream
->parent
.stream_id
= stream_id
;
1233 viewer_stream
->ctf_stream_id
= stream_id
;
1238 pos
->packet_size
= cur_index
->packet_size
;
1239 pos
->content_size
= cur_index
->content_size
;
1240 pos
->mmap_base_offset
= 0;
1241 if (cur_index
->offset
== EOF
) {
1247 if (cur_index
->content_size
== 0) {
1248 if (file_stream
->parent
.stream_class
) {
1249 file_stream
->parent
.cycles_timestamp
=
1250 cur_index
->ts_cycles
.timestamp_end
;
1251 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
1252 &file_stream
->parent
,
1253 cur_index
->ts_cycles
.timestamp_end
);
1256 if (file_stream
->parent
.stream_class
) {
1257 /* Convert the timestamps and append to the real_index. */
1258 cur_index
->ts_real
.timestamp_begin
= ctf_get_real_timestamp(
1259 &file_stream
->parent
,
1260 cur_index
->ts_cycles
.timestamp_begin
);
1261 cur_index
->ts_real
.timestamp_end
= ctf_get_real_timestamp(
1262 &file_stream
->parent
,
1263 cur_index
->ts_cycles
.timestamp_end
);
1266 ctf_update_current_packet_index(&file_stream
->parent
,
1267 prev_index
, cur_index
);
1269 file_stream
->parent
.cycles_timestamp
=
1270 cur_index
->ts_cycles
.timestamp_begin
;
1271 file_stream
->parent
.real_timestamp
=
1272 cur_index
->ts_real
.timestamp_begin
;
1275 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
1279 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
1281 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
1283 cur_index
->packet_size
/ CHAR_BIT
);
1286 } else if (ret
< 0) {
1288 if (!lttng_live_should_quit()) {
1289 fprintf(stderr
, "[error] get_data_packet failed\n");
1293 viewer_stream
->data_pending
= 0;
1295 read_packet_header(pos
, file_stream
);
1301 int lttng_live_create_viewer_session(struct lttng_live_ctx
*ctx
)
1303 struct lttng_viewer_cmd cmd
;
1304 struct lttng_viewer_create_session_response resp
;
1308 if (lttng_live_should_quit()) {
1313 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
1315 cmd
.cmd_version
= 0;
1317 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1319 perror("[error] Error sending cmd");
1322 assert(ret_len
== sizeof(cmd
));
1324 ret_len
= lttng_live_recv(ctx
->control_sock
, &resp
, sizeof(resp
));
1326 fprintf(stderr
, "[error] Remote side has closed connection\n");
1330 perror("[error] Error receiving create session reply");
1333 assert(ret_len
== sizeof(resp
));
1335 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
1336 fprintf(stderr
, "[error] Error creating viewer session\n");
1348 int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
1350 struct bt_context
*bt_ctx
= user_data
;
1351 struct lttng_live_ctf_trace
*trace
= value
;
1354 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
1356 fprintf(stderr
, "[error] removing trace from context\n");
1358 /* remove the key/value pair from the HT. */
1363 void add_traces(gpointer key
, gpointer value
, gpointer user_data
)
1366 struct bt_context
*bt_ctx
= user_data
;
1367 struct lttng_live_ctf_trace
*trace
= value
;
1368 struct lttng_live_viewer_stream
*stream
;
1369 struct bt_mmap_stream
*new_mmap_stream
;
1370 struct bt_mmap_stream_list mmap_list
;
1371 struct lttng_live_ctx
*ctx
= NULL
;
1372 struct bt_trace_descriptor
*td
;
1373 struct bt_trace_handle
*handle
;
1376 * We don't know how many streams we will receive for a trace, so
1377 * once we are done receiving the traces, we add all the traces
1378 * received to the bt_context.
1379 * We can receive streams during the attach command or the
1380 * get_new_streams, so we have to make sure not to add multiple
1381 * times the same traces.
1382 * If a trace is already in the context, we just skip this function.
1387 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1389 for (i
= 0; i
< trace
->streams
->len
; i
++) {
1390 stream
= g_ptr_array_index(trace
->streams
, i
);
1391 ctx
= stream
->session
->ctx
;
1393 if (!stream
->metadata_flag
) {
1394 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
1395 new_mmap_stream
->priv
= (void *) stream
;
1396 new_mmap_stream
->fd
= -1;
1397 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1399 char *metadata_buf
= NULL
;
1401 /* Get all possible metadata before starting */
1402 ret
= get_new_metadata(ctx
, stream
, &metadata_buf
);
1407 if (!stream
->metadata_len
) {
1408 fprintf(stderr
, "[error] empty metadata\n");
1414 trace
->metadata_fp
= babeltrace_fmemopen(metadata_buf
,
1415 stream
->metadata_len
, "rb");
1416 if (!trace
->metadata_fp
) {
1417 perror("Metadata fmemopen2");
1425 if (!trace
->metadata_fp
) {
1426 fprintf(stderr
, "[error] No metadata stream opened\n");
1430 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
1431 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
1433 fprintf(stderr
, "[error] Error adding trace\n");
1436 trace
->metadata_stream
->metadata_len
= 0;
1438 handle
= (struct bt_trace_handle
*) g_hash_table_lookup(
1439 bt_ctx
->trace_handles
,
1440 (gpointer
) (unsigned long) ret
);
1442 trace
->handle
= handle
;
1443 if (bt_ctx
->current_iterator
) {
1444 bt_iter_add_trace(bt_ctx
->current_iterator
, td
);
1447 trace
->trace_id
= ret
;
1453 bt_context_put(bt_ctx
);
1459 * Request new streams for a session.
1460 * Returns the number of streams received or a negative value on error.
1462 int lttng_live_get_new_streams(struct lttng_live_ctx
*ctx
, uint64_t id
)
1464 struct lttng_viewer_cmd cmd
;
1465 struct lttng_viewer_new_streams_request rq
;
1466 struct lttng_viewer_new_streams_response rp
;
1467 struct lttng_viewer_stream stream
;
1468 int ret
, i
, nb_streams
= 0;
1470 uint32_t stream_count
;
1472 if (lttng_live_should_quit()) {
1477 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1478 cmd
.data_size
= sizeof(rq
);
1479 cmd
.cmd_version
= 0;
1481 memset(&rq
, 0, sizeof(rq
));
1482 rq
.session_id
= htobe64(id
);
1484 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1486 perror("[error] Error sending cmd");
1489 assert(ret_len
== sizeof(cmd
));
1491 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
1493 perror("[error] Error sending get_new_streams request");
1496 assert(ret_len
== sizeof(rq
));
1498 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
1500 fprintf(stderr
, "[error] Remote side has closed connection\n");
1504 perror("[error] Error receiving get_new_streams response");
1507 assert(ret_len
== sizeof(rp
));
1509 switch(be32toh(rp
.status
)) {
1510 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1512 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1515 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1516 ret
= -LTTNG_VIEWER_NEW_STREAMS_HUP
;
1518 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1519 fprintf(stderr
, "[error] get_new_streams error\n");
1522 fprintf(stderr
, "[error] Unknown return code %u\n",
1523 be32toh(rp
.status
));
1527 stream_count
= be32toh(rp
.streams_count
);
1528 ctx
->session
->stream_count
+= stream_count
;
1530 * When the session is created but not started, we do an active wait
1531 * until it starts. It allows the viewer to start processing the trace
1532 * as soon as the session starts.
1534 if (ctx
->session
->stream_count
== 0) {
1538 printf_verbose("Waiting for %" PRIu64
" streams:\n",
1539 ctx
->session
->stream_count
);
1540 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
1541 ctx
->session
->stream_count
);
1542 for (i
= 0; i
< stream_count
; i
++) {
1543 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
, sizeof(stream
));
1545 fprintf(stderr
, "[error] Remote side has closed connection\n");
1549 perror("[error] Error receiving stream");
1552 assert(ret_len
== sizeof(stream
));
1553 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
1554 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
1556 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
1557 be64toh(stream
.id
), stream
.path_name
,
1558 stream
.channel_name
);
1559 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
1560 ctx
->session
->streams
[i
].session
= ctx
->session
;
1562 ctx
->session
->streams
[i
].mmap_size
= 0;
1563 ctx
->session
->streams
[i
].ctf_stream_id
= -1ULL;
1565 if (be32toh(stream
.metadata_flag
)) {
1566 ctx
->session
->streams
[i
].metadata_flag
= 1;
1568 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
1569 be64toh(stream
.ctf_trace_id
));
1584 int lttng_live_read(struct lttng_live_ctx
*ctx
)
1588 struct bt_ctf_iter
*iter
;
1589 const struct bt_ctf_event
*event
;
1590 struct bt_iter_pos begin_pos
;
1591 struct bt_trace_descriptor
*td_write
;
1592 struct bt_format
*fmt_write
;
1593 struct ctf_text_stream_pos
*sout
;
1597 ctx
->bt_ctx
= bt_context_create();
1599 fprintf(stderr
, "[error] bt_context_create allocation\n");
1604 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
1606 fprintf(stderr
, "[error] ctf-text error\n");
1610 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
1612 fprintf(stderr
, "[error] Error opening output trace\n");
1616 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
1618 if (!sout
->parent
.event_cb
) {
1623 ret
= lttng_live_create_viewer_session(ctx
);
1628 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
1629 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
1630 printf_verbose("Attaching to session %lu\n", id
);
1631 ret
= lttng_live_attach_session(ctx
, id
);
1632 printf_verbose("Attaching session returns %d\n", ret
);
1634 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
1635 fprintf(stderr
, "[error] Unknown session ID\n");
1642 * As long as the session is active, we try to get new streams.
1648 if (lttng_live_should_quit()) {
1653 while (!ctx
->session
->stream_count
) {
1654 if (lttng_live_should_quit()
1655 || ctx
->session_ids
->len
== 0) {
1659 ret
= ask_new_streams(ctx
);
1663 if (!ctx
->session
->stream_count
) {
1664 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1668 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
,
1672 begin_pos
.type
= BT_SEEK_BEGIN
;
1673 iter
= bt_ctf_iter_create(ctx
->bt_ctx
, &begin_pos
, NULL
);
1675 if (lttng_live_should_quit()) {
1679 fprintf(stderr
, "[error] Iterator creation error\n");
1684 if (lttng_live_should_quit()) {
1688 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
1689 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
1694 ret
= sout
->parent
.event_cb(&sout
->parent
,
1695 event
->parent
->stream
);
1697 fprintf(stderr
, "[error] Writing "
1702 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1707 bt_ctf_iter_destroy(iter
);
1709 ret
= check_requirements(ctx
->bt_ctx
);
1710 if (ret
< 0 && !valid_trace
) {
1711 fprintf(stderr
, "[error] some mandatory contexts "
1712 "were missing, exiting.\n");
1716 if (!opt_textdump
) {
1717 #ifdef HAVE_LIBNCURSES
1718 pthread_create(&display_thread
, NULL
, ncurses_display
,
1720 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1723 printf("Ncurses support not compiled, please install "
1724 "the missing dependencies and recompile\n");
1728 iter_trace(ctx
->bt_ctx
);
1729 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
,
1730 del_traces
, ctx
->bt_ctx
);
1731 ctx
->session
->stream_count
= 0;
1737 bt_context_put(ctx
->bt_ctx
);
1740 if (lttng_live_should_quit()) {