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 <formats/ctf/events-private.h>
53 #include <babeltrace/endian.h>
54 #include <babeltrace/compat/memstream.h>
56 #include "lttng-live.h"
57 #include "lttng-viewer-abi.h"
59 #define ACTIVE_POLL_DELAY 100 /* ms */
62 * Memory allocation zeroed
64 #define zmalloc(x) calloc(1, x)
67 #define max_t(type, a, b) \
68 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
71 static void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
,
72 size_t index
, int whence
);
73 static int add_traces(struct lttng_live_ctx
*ctx
);
74 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
);
75 static int get_new_metadata(struct lttng_live_ctx
*ctx
,
76 struct lttng_live_viewer_stream
*viewer_stream
,
80 ssize_t
lttng_live_recv(int fd
, void *buf
, size_t len
)
83 size_t copied
= 0, to_copy
= len
;
86 ret
= recv(fd
, buf
+ copied
, to_copy
, 0);
88 assert(ret
<= to_copy
);
92 } while ((ret
> 0 && to_copy
> 0)
93 || (ret
< 0 && errno
== EINTR
));
96 /* ret = 0 means orderly shutdown, ret < 0 is error. */
101 ssize_t
lttng_live_send(int fd
, const void *buf
, size_t len
)
106 ret
= send(fd
, buf
, len
, MSG_NOSIGNAL
);
107 } while (ret
< 0 && errno
== EINTR
);
111 int lttng_live_connect_viewer(struct lttng_live_ctx
*ctx
)
113 struct hostent
*host
;
114 struct sockaddr_in server_addr
;
117 if (lttng_live_should_quit()) {
122 host
= gethostbyname(ctx
->relay_hostname
);
124 fprintf(stderr
, "[error] Cannot lookup hostname %s\n",
125 ctx
->relay_hostname
);
129 if ((ctx
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
134 server_addr
.sin_family
= AF_INET
;
135 server_addr
.sin_port
= htons(ctx
->port
);
136 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
137 bzero(&(server_addr
.sin_zero
), 8);
139 if (connect(ctx
->control_sock
, (struct sockaddr
*) &server_addr
,
140 sizeof(struct sockaddr
)) == -1) {
151 fprintf(stderr
, "[error] Connection failed\n");
155 int lttng_live_establish_connection(struct lttng_live_ctx
*ctx
)
157 struct lttng_viewer_cmd cmd
;
158 struct lttng_viewer_connect connect
;
162 if (lttng_live_should_quit()) {
167 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
168 cmd
.data_size
= sizeof(connect
);
171 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
172 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
173 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
174 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
176 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
178 perror("[error] Error sending cmd");
181 assert(ret_len
== sizeof(cmd
));
183 ret_len
= lttng_live_send(ctx
->control_sock
, &connect
, sizeof(connect
));
185 perror("[error] Error sending version");
188 assert(ret_len
== sizeof(connect
));
190 ret_len
= lttng_live_recv(ctx
->control_sock
, &connect
, sizeof(connect
));
192 fprintf(stderr
, "[error] Remote side has closed connection\n");
196 perror("[error] Error receiving version");
199 assert(ret_len
== sizeof(connect
));
201 printf_verbose("Received viewer session ID : %" PRIu64
"\n",
202 be64toh(connect
.viewer_session_id
));
203 printf_verbose("Relayd version : %u.%u\n", be32toh(connect
.major
),
204 be32toh(connect
.minor
));
206 if (LTTNG_LIVE_MAJOR
!= be32toh(connect
.major
)) {
207 fprintf(stderr
, "[error] Incompatible lttng-relayd protocol\n");
210 /* Use the smallest protocol version implemented. */
211 if (LTTNG_LIVE_MINOR
> be32toh(connect
.minor
)) {
212 ctx
->minor
= be32toh(connect
.minor
);
214 ctx
->minor
= LTTNG_LIVE_MINOR
;
216 ctx
->major
= LTTNG_LIVE_MAJOR
;
222 fprintf(stderr
, "[error] Unable to establish connection\n");
227 void free_session_list(GPtrArray
*session_list
)
230 struct lttng_live_relay_session
*relay_session
;
232 for (i
= 0; i
< session_list
->len
; i
++) {
233 relay_session
= g_ptr_array_index(session_list
, i
);
234 free(relay_session
->name
);
235 free(relay_session
->hostname
);
237 g_ptr_array_free(session_list
, TRUE
);
241 void print_session_list(GPtrArray
*session_list
, const char *path
)
244 struct lttng_live_relay_session
*relay_session
;
246 for (i
= 0; i
< session_list
->len
; i
++) {
247 relay_session
= g_ptr_array_index(session_list
, i
);
248 fprintf(stdout
, "%s/host/%s/%s (timer = %u, "
249 "%u stream(s), %u client(s) connected)\n",
250 path
, relay_session
->hostname
,
251 relay_session
->name
, relay_session
->timer
,
252 relay_session
->streams
, relay_session
->clients
);
257 void update_session_list(GPtrArray
*session_list
, char *hostname
,
258 char *session_name
, uint32_t streams
, uint32_t clients
,
262 struct lttng_live_relay_session
*relay_session
;
264 for (i
= 0; i
< session_list
->len
; i
++) {
265 relay_session
= g_ptr_array_index(session_list
, i
);
266 if ((strncmp(relay_session
->hostname
, hostname
, MAXNAMLEN
) == 0) &&
267 strncmp(relay_session
->name
,
268 session_name
, MAXNAMLEN
) == 0) {
269 relay_session
->streams
+= streams
;
270 if (relay_session
->clients
< clients
)
271 relay_session
->clients
= clients
;
279 relay_session
= g_new0(struct lttng_live_relay_session
, 1);
280 relay_session
->hostname
= strndup(hostname
, MAXNAMLEN
);
281 relay_session
->name
= strndup(session_name
, MAXNAMLEN
);
282 relay_session
->clients
= clients
;
283 relay_session
->streams
= streams
;
284 relay_session
->timer
= timer
;
285 g_ptr_array_add(session_list
, relay_session
);
288 int lttng_live_list_sessions(struct lttng_live_ctx
*ctx
, const char *path
)
290 struct lttng_viewer_cmd cmd
;
291 struct lttng_viewer_list_sessions list
;
292 struct lttng_viewer_session lsession
;
293 int i
, ret
, sessions_count
, print_list
= 0;
296 GPtrArray
*session_list
= NULL
;
298 if (lttng_live_should_quit()) {
303 if (strlen(ctx
->session_name
) == 0) {
305 session_list
= g_ptr_array_new();
308 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
312 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
314 perror("[error] Error sending cmd");
317 assert(ret_len
== sizeof(cmd
));
319 ret_len
= lttng_live_recv(ctx
->control_sock
, &list
, sizeof(list
));
321 fprintf(stderr
, "[error] Remote side has closed connection\n");
325 perror("[error] Error receiving session list");
328 assert(ret_len
== sizeof(list
));
330 sessions_count
= be32toh(list
.sessions_count
);
331 for (i
= 0; i
< sessions_count
; i
++) {
332 ret_len
= lttng_live_recv(ctx
->control_sock
, &lsession
, sizeof(lsession
));
334 fprintf(stderr
, "[error] Remote side has closed connection\n");
338 perror("[error] Error receiving session");
341 assert(ret_len
== sizeof(lsession
));
342 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
343 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
344 session_id
= be64toh(lsession
.id
);
347 update_session_list(session_list
,
349 lsession
.session_name
,
350 be32toh(lsession
.streams
),
351 be32toh(lsession
.clients
),
352 be32toh(lsession
.live_timer
));
354 if ((strncmp(lsession
.session_name
, ctx
->session_name
,
355 MAXNAMLEN
) == 0) && (strncmp(lsession
.hostname
,
356 ctx
->traced_hostname
, MAXNAMLEN
) == 0)) {
357 printf_verbose("Reading from session %" PRIu64
"\n",
359 g_array_append_val(ctx
->session_ids
,
366 print_session_list(session_list
, path
);
367 free_session_list(session_list
);
374 fprintf(stderr
, "[error] Unable to list sessions\n");
378 int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream
*stream
,
379 uint64_t ctf_trace_id
)
381 struct lttng_live_ctf_trace
*trace
;
384 trace
= g_hash_table_lookup(stream
->session
->ctf_traces
,
387 trace
= g_new0(struct lttng_live_ctf_trace
, 1);
388 trace
->ctf_trace_id
= ctf_trace_id
;
389 trace
->streams
= g_ptr_array_new();
390 g_hash_table_insert(stream
->session
->ctf_traces
,
391 &trace
->ctf_trace_id
,
394 if (stream
->metadata_flag
)
395 trace
->metadata_stream
= stream
;
397 stream
->ctf_trace
= trace
;
398 g_ptr_array_add(trace
->streams
, stream
);
404 int open_metadata_fp_write(struct lttng_live_viewer_stream
*stream
,
405 char **metadata_buf
, size_t *size
)
409 stream
->metadata_fp_write
=
410 babeltrace_open_memstream(metadata_buf
, size
);
411 if (!stream
->metadata_fp_write
) {
412 perror("Metadata open_memstream");
419 int lttng_live_attach_session(struct lttng_live_ctx
*ctx
, uint64_t id
)
421 struct lttng_viewer_cmd cmd
;
422 struct lttng_viewer_attach_session_request rq
;
423 struct lttng_viewer_attach_session_response rp
;
424 struct lttng_viewer_stream stream
;
428 if (lttng_live_should_quit()) {
433 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
434 cmd
.data_size
= sizeof(rq
);
437 memset(&rq
, 0, sizeof(rq
));
438 rq
.session_id
= htobe64(id
);
439 // TODO: add cmd line parameter to select seek beginning
440 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
441 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
443 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
445 perror("[error] Error sending cmd");
448 assert(ret_len
== sizeof(cmd
));
450 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
452 perror("[error] Error sending attach request");
455 assert(ret_len
== sizeof(rq
));
457 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
459 fprintf(stderr
, "[error] Remote side has closed connection\n");
463 perror("[error] Error receiving attach response");
466 assert(ret_len
== sizeof(rp
));
468 switch(be32toh(rp
.status
)) {
469 case LTTNG_VIEWER_ATTACH_OK
:
471 case LTTNG_VIEWER_ATTACH_UNK
:
472 ret
= -LTTNG_VIEWER_ATTACH_UNK
;
474 case LTTNG_VIEWER_ATTACH_ALREADY
:
475 fprintf(stderr
, "[error] There is already a viewer attached to this session\n");
477 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
478 fprintf(stderr
, "[error] Not a live session\n");
480 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
481 fprintf(stderr
, "[error] Wrong seek parameter\n");
484 fprintf(stderr
, "[error] Unknown attach return code %u\n",
488 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
492 ctx
->session
->stream_count
+= be32toh(rp
.streams_count
);
494 * When the session is created but not started, we do an active wait
495 * until it starts. It allows the viewer to start processing the trace
496 * as soon as the session starts.
498 if (ctx
->session
->stream_count
== 0) {
502 printf_verbose("Waiting for %" PRIu64
" streams:\n",
503 ctx
->session
->stream_count
);
504 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
505 ctx
->session
->stream_count
);
506 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
507 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
, sizeof(stream
));
509 fprintf(stderr
, "[error] Remote side has closed connection\n");
513 perror("[error] Error receiving stream");
516 assert(ret_len
== sizeof(stream
));
517 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
518 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
520 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
521 be64toh(stream
.id
), stream
.path_name
,
522 stream
.channel_name
);
523 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
524 ctx
->session
->streams
[i
].session
= ctx
->session
;
526 ctx
->session
->streams
[i
].mmap_size
= 0;
527 ctx
->session
->streams
[i
].ctf_stream_id
= -1ULL;
529 if (be32toh(stream
.metadata_flag
)) {
530 ctx
->session
->streams
[i
].metadata_flag
= 1;
532 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
533 be64toh(stream
.ctf_trace_id
));
548 * Ask the relay for new streams.
550 * Returns the number of new streams received or a negative value on error.
553 int ask_new_streams(struct lttng_live_ctx
*ctx
)
555 int i
, ret
= 0, nb_streams
= 0;
559 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
560 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
561 ret
= lttng_live_get_new_streams(ctx
, id
);
562 printf_verbose("Asking for new streams returns %d\n", ret
);
564 if (lttng_live_should_quit()) {
567 if (ret
== -LTTNG_VIEWER_NEW_STREAMS_HUP
) {
568 printf_verbose("Session %" PRIu64
" closed\n",
571 * The streams have already been closed during
572 * the reading, so we only need to get rid of
573 * the trace in our internal table of sessions.
575 g_array_remove_index(ctx
->session_ids
, i
);
577 * We can't continue iterating on the g_array
578 * after a remove, we have to start again.
596 int append_metadata(struct lttng_live_ctx
*ctx
,
597 struct lttng_live_viewer_stream
*viewer_stream
)
600 struct lttng_live_viewer_stream
*metadata
;
601 char *metadata_buf
= NULL
;
603 printf_verbose("get_next_index: new metadata needed\n");
604 ret
= get_new_metadata(ctx
, viewer_stream
, &metadata_buf
);
610 metadata
= viewer_stream
->ctf_trace
->metadata_stream
;
611 metadata
->ctf_trace
->metadata_fp
=
612 babeltrace_fmemopen(metadata_buf
,
613 metadata
->metadata_len
, "rb");
614 if (!metadata
->ctf_trace
->metadata_fp
) {
615 perror("Metadata fmemopen");
620 ret
= ctf_append_trace_metadata(
621 viewer_stream
->ctf_trace
->handle
->td
,
622 metadata
->ctf_trace
->metadata_fp
);
623 /* We accept empty metadata packets */
624 if (ret
!= 0 && ret
!= -ENOENT
) {
625 fprintf(stderr
, "[error] Appending metadata\n");
635 int get_data_packet(struct lttng_live_ctx
*ctx
,
636 struct ctf_stream_pos
*pos
,
637 struct lttng_live_viewer_stream
*stream
, uint64_t offset
,
640 struct lttng_viewer_cmd cmd
;
641 struct lttng_viewer_get_packet rq
;
642 struct lttng_viewer_trace_packet rp
;
647 if (lttng_live_should_quit()) {
651 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
652 cmd
.data_size
= sizeof(rq
);
655 memset(&rq
, 0, sizeof(rq
));
656 rq
.stream_id
= htobe64(stream
->id
);
657 rq
.offset
= htobe64(offset
);
658 rq
.len
= htobe32(len
);
660 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
662 perror("[error] Error sending cmd");
665 assert(ret_len
== sizeof(cmd
));
667 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
669 perror("[error] Error sending get_data_packet request");
672 assert(ret_len
== sizeof(rq
));
674 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
676 fprintf(stderr
, "[error] Remote side has closed connection\n");
680 perror("[error] Error receiving data response");
683 if (ret_len
!= sizeof(rp
)) {
684 fprintf(stderr
, "[error] get_data_packet: expected %zu"
685 ", received %zd\n", sizeof(rp
),
690 rp
.flags
= be32toh(rp
.flags
);
692 switch (be32toh(rp
.status
)) {
693 case LTTNG_VIEWER_GET_PACKET_OK
:
694 len
= be32toh(rp
.len
);
695 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
698 case LTTNG_VIEWER_GET_PACKET_RETRY
:
699 /* Unimplemented by relay daemon */
700 printf_verbose("get_data_packet: retry\n");
702 case LTTNG_VIEWER_GET_PACKET_ERR
:
703 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
704 printf_verbose("get_data_packet: new metadata needed\n");
705 ret
= append_metadata(ctx
, stream
);
709 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
710 printf_verbose("get_data_packet: new streams needed\n");
711 ret
= ask_new_streams(ctx
);
714 } else if (ret
> 0) {
715 ret
= add_traces(ctx
);
721 if (rp
.flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
722 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
725 fprintf(stderr
, "[error] get_data_packet: error\n");
727 case LTTNG_VIEWER_GET_PACKET_EOF
:
731 printf_verbose("get_data_packet: unknown\n");
739 if (len
> stream
->mmap_size
) {
742 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
745 ret
= munmap_align(pos
->base_mma
);
747 perror("[error] Unable to unmap old base");
750 pos
->base_mma
= NULL
;
752 pos
->base_mma
= mmap_align(new_size
,
753 PROT_READ
| PROT_WRITE
,
754 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
755 if (pos
->base_mma
== MAP_FAILED
) {
756 perror("[error] mmap error");
757 pos
->base_mma
= NULL
;
761 stream
->mmap_size
= new_size
;
762 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
766 ret_len
= lttng_live_recv(ctx
->control_sock
,
767 mmap_align_addr(pos
->base_mma
), len
);
769 fprintf(stderr
, "[error] Remote side has closed connection\n");
773 perror("[error] Error receiving trace packet");
776 assert(ret_len
== len
);
786 int get_one_metadata_packet(struct lttng_live_ctx
*ctx
,
787 struct lttng_live_viewer_stream
*metadata_stream
)
791 struct lttng_viewer_cmd cmd
;
792 struct lttng_viewer_get_metadata rq
;
793 struct lttng_viewer_metadata_packet rp
;
797 if (lttng_live_should_quit()) {
802 rq
.stream_id
= htobe64(metadata_stream
->id
);
803 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
804 cmd
.data_size
= sizeof(rq
);
807 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
809 perror("[error] Error sending cmd");
812 assert(ret_len
== sizeof(cmd
));
814 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
816 perror("[error] Error sending get_metadata request");
819 assert(ret_len
== sizeof(rq
));
821 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
823 fprintf(stderr
, "[error] Remote side has closed connection\n");
827 perror("[error] Error receiving metadata response");
830 assert(ret_len
== sizeof(rp
));
832 switch (be32toh(rp
.status
)) {
833 case LTTNG_VIEWER_METADATA_OK
:
834 printf_verbose("get_metadata : OK\n");
836 case LTTNG_VIEWER_NO_NEW_METADATA
:
837 printf_verbose("get_metadata : NO NEW\n");
840 case LTTNG_VIEWER_METADATA_ERR
:
841 printf_verbose("get_metadata : ERR\n");
844 printf_verbose("get_metadata : UNKNOWN\n");
848 len
= be64toh(rp
.len
);
849 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
856 perror("relay data zmalloc");
859 ret_len
= lttng_live_recv(ctx
->control_sock
, data
, len
);
861 fprintf(stderr
, "[error] Remote side has closed connection\n");
862 goto error_free_data
;
865 perror("[error] Error receiving trace packet");
866 goto error_free_data
;
868 assert(ret_len
== len
);
871 ret_len
= fwrite(data
, 1, len
,
872 metadata_stream
->metadata_fp_write
);
873 } while (ret_len
< 0 && errno
== EINTR
);
875 fprintf(stderr
, "[error] Writing in the metadata fp\n");
876 goto error_free_data
;
878 assert(ret_len
== len
);
879 metadata_stream
->metadata_len
+= len
;
892 * Return 0 on success, a negative value on error.
895 int get_new_metadata(struct lttng_live_ctx
*ctx
,
896 struct lttng_live_viewer_stream
*viewer_stream
,
900 struct lttng_live_viewer_stream
*metadata_stream
;
901 size_t size
, len_read
= 0;
903 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
904 if (!metadata_stream
) {
905 fprintf(stderr
, "[error] No metadata stream\n");
909 metadata_stream
->metadata_len
= 0;
910 ret
= open_metadata_fp_write(metadata_stream
, metadata_buf
, &size
);
916 if (lttng_live_should_quit()) {
921 * get_one_metadata_packet returns the number of bytes
922 * received, 0 when we have received everything, a
923 * negative value on error.
925 ret
= get_one_metadata_packet(ctx
, metadata_stream
);
930 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
932 } while (ret
> 0 || !len_read
);
934 if (fclose(metadata_stream
->metadata_fp_write
))
936 metadata_stream
->metadata_fp_write
= NULL
;
943 * Assign the fields from a lttng_viewer_index to a packet_index.
946 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
947 struct packet_index
*pindex
)
952 pindex
->offset
= be64toh(lindex
->offset
);
953 pindex
->packet_size
= be64toh(lindex
->packet_size
);
954 pindex
->content_size
= be64toh(lindex
->content_size
);
955 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
956 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
957 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
961 * Get one index for a stream.
963 * Returns 0 on success or a negative value on error.
966 int get_next_index(struct lttng_live_ctx
*ctx
,
967 struct lttng_live_viewer_stream
*viewer_stream
,
968 struct packet_index
*index
, uint64_t *stream_id
)
970 struct lttng_viewer_cmd cmd
;
971 struct lttng_viewer_get_next_index rq
;
974 struct lttng_viewer_index
*rp
= &viewer_stream
->current_index
;
976 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
977 cmd
.data_size
= sizeof(rq
);
980 memset(&rq
, 0, sizeof(rq
));
981 rq
.stream_id
= htobe64(viewer_stream
->id
);
984 if (lttng_live_should_quit()) {
988 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
990 perror("[error] Error sending cmd");
993 assert(ret_len
== sizeof(cmd
));
995 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
997 perror("[error] Error sending get_next_index request");
1000 assert(ret_len
== sizeof(rq
));
1002 ret_len
= lttng_live_recv(ctx
->control_sock
, rp
, sizeof(*rp
));
1004 fprintf(stderr
, "[error] Remote side has closed connection\n");
1008 perror("[error] Error receiving index response");
1011 assert(ret_len
== sizeof(*rp
));
1013 rp
->flags
= be32toh(rp
->flags
);
1015 switch (be32toh(rp
->status
)) {
1016 case LTTNG_VIEWER_INDEX_INACTIVE
:
1017 printf_verbose("get_next_index: inactive\n");
1018 memset(index
, 0, sizeof(struct packet_index
));
1019 index
->ts_cycles
.timestamp_end
= be64toh(rp
->timestamp_end
);
1020 *stream_id
= be64toh(rp
->stream_id
);
1022 case LTTNG_VIEWER_INDEX_OK
:
1023 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
1024 rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
1025 lttng_index_to_packet_index(rp
, index
);
1026 *stream_id
= be64toh(rp
->stream_id
);
1027 viewer_stream
->data_pending
= 1;
1029 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1030 ret
= append_metadata(ctx
, viewer_stream
);
1034 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1035 printf_verbose("get_next_index: need new streams\n");
1036 ret
= ask_new_streams(ctx
);
1039 } else if (ret
> 0) {
1040 ret
= add_traces(ctx
);
1047 case LTTNG_VIEWER_INDEX_RETRY
:
1048 printf_verbose("get_next_index: retry\n");
1049 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1051 case LTTNG_VIEWER_INDEX_HUP
:
1052 printf_verbose("get_next_index: stream hung up\n");
1053 viewer_stream
->id
= -1ULL;
1054 index
->offset
= EOF
;
1055 ctx
->session
->stream_count
--;
1057 case LTTNG_VIEWER_INDEX_ERR
:
1058 fprintf(stderr
, "[error] get_next_index: error\n");
1061 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
1073 void read_packet_header(struct ctf_stream_pos
*pos
,
1074 struct ctf_file_stream
*file_stream
)
1078 /* update trace_packet_header and stream_packet_context */
1079 if (!(pos
->prot
& PROT_WRITE
) &&
1080 file_stream
->parent
.trace_packet_header
) {
1081 /* Read packet header */
1082 ret
= generic_rw(&pos
->parent
,
1083 &file_stream
->parent
.trace_packet_header
->p
);
1086 fprintf(stderr
, "[error] trace packet "
1087 "header read failed\n");
1091 if (!(pos
->prot
& PROT_WRITE
) &&
1092 file_stream
->parent
.stream_packet_context
) {
1093 /* Read packet context */
1094 ret
= generic_rw(&pos
->parent
,
1095 &file_stream
->parent
.stream_packet_context
->p
);
1098 fprintf(stderr
, "[error] stream packet "
1099 "context read failed\n");
1103 pos
->data_offset
= pos
->offset
;
1110 * Handle the seek parameters.
1111 * Returns 0 if the packet_seek can continue, a positive value to
1112 * cleanly exit the packet_seek, a negative value on error.
1115 int handle_seek_position(size_t index
, int whence
,
1116 struct lttng_live_viewer_stream
*viewer_stream
,
1117 struct ctf_stream_pos
*pos
,
1118 struct ctf_file_stream
*file_stream
)
1128 * We only allow to seek to 0.
1131 fprintf(stderr
, "[error] Arbitrary seek in lttng-live "
1132 "trace not supported\n");
1142 fprintf(stderr
, "[error] Invalid seek parameter\n");
1151 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
1154 struct ctf_stream_pos
*pos
;
1155 struct ctf_file_stream
*file_stream
;
1156 struct packet_index
*prev_index
= NULL
, *cur_index
;
1157 struct lttng_live_viewer_stream
*viewer_stream
;
1158 struct lttng_live_session
*session
;
1159 uint64_t stream_id
= -1ULL;
1162 pos
= ctf_pos(stream_pos
);
1163 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
1164 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
1165 session
= viewer_stream
->session
;
1167 ret
= handle_seek_position(index
, whence
, viewer_stream
, pos
,
1174 switch (pos
->packet_index
->len
) {
1176 g_array_set_size(pos
->packet_index
, 1);
1177 cur_index
= &g_array_index(pos
->packet_index
,
1178 struct packet_index
, 0);
1181 g_array_set_size(pos
->packet_index
, 2);
1182 prev_index
= &g_array_index(pos
->packet_index
,
1183 struct packet_index
, 0);
1184 cur_index
= &g_array_index(pos
->packet_index
,
1185 struct packet_index
, 1);
1188 g_array_index(pos
->packet_index
,
1189 struct packet_index
, 0) =
1190 g_array_index(pos
->packet_index
,
1191 struct packet_index
, 1);
1192 prev_index
= &g_array_index(pos
->packet_index
,
1193 struct packet_index
, 0);
1194 cur_index
= &g_array_index(pos
->packet_index
,
1195 struct packet_index
, 1);
1202 if (viewer_stream
->data_pending
) {
1203 lttng_index_to_packet_index(&viewer_stream
->current_index
, cur_index
);
1205 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
1206 ret
= get_next_index(session
->ctx
, viewer_stream
, cur_index
, &stream_id
);
1209 if (!lttng_live_should_quit()) {
1210 fprintf(stderr
, "[error] get_next_index failed\n");
1214 printf_verbose("Index received : packet_size : %" PRIu64
1215 ", offset %" PRIu64
", content_size %" PRIu64
1216 ", timestamp_end : %" PRIu64
"\n",
1217 cur_index
->packet_size
, cur_index
->offset
,
1218 cur_index
->content_size
,
1219 cur_index
->ts_cycles
.timestamp_end
);
1224 * On the first time we receive an index, the stream_id needs to
1225 * be set for the stream in order to use it, we don't want any
1226 * data at this stage.
1228 if (file_stream
->parent
.stream_id
== -1ULL) {
1230 * Warning: with lttng-tools < 2.4.2, the beacon does not
1231 * contain the real stream ID, it is memset to 0, so this
1232 * might create a problem when a session has multiple
1233 * channels. We can't detect it at this stage, lttng-tools
1234 * has to be upgraded to fix this problem.
1236 printf_verbose("Assigning stream_id %" PRIu64
"\n",
1238 file_stream
->parent
.stream_id
= stream_id
;
1239 viewer_stream
->ctf_stream_id
= stream_id
;
1244 pos
->packet_size
= cur_index
->packet_size
;
1245 pos
->content_size
= cur_index
->content_size
;
1246 pos
->mmap_base_offset
= 0;
1247 if (cur_index
->offset
== EOF
) {
1253 if (cur_index
->content_size
== 0) {
1254 if (file_stream
->parent
.stream_class
) {
1255 file_stream
->parent
.cycles_timestamp
=
1256 cur_index
->ts_cycles
.timestamp_end
;
1257 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
1258 &file_stream
->parent
,
1259 cur_index
->ts_cycles
.timestamp_end
);
1262 if (file_stream
->parent
.stream_class
) {
1263 /* Convert the timestamps and append to the real_index. */
1264 cur_index
->ts_real
.timestamp_begin
= ctf_get_real_timestamp(
1265 &file_stream
->parent
,
1266 cur_index
->ts_cycles
.timestamp_begin
);
1267 cur_index
->ts_real
.timestamp_end
= ctf_get_real_timestamp(
1268 &file_stream
->parent
,
1269 cur_index
->ts_cycles
.timestamp_end
);
1272 ctf_update_current_packet_index(&file_stream
->parent
,
1273 prev_index
, cur_index
);
1275 file_stream
->parent
.cycles_timestamp
=
1276 cur_index
->ts_cycles
.timestamp_begin
;
1277 file_stream
->parent
.real_timestamp
=
1278 cur_index
->ts_real
.timestamp_begin
;
1281 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
1285 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
1287 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
1289 cur_index
->packet_size
/ CHAR_BIT
);
1292 } else if (ret
< 0) {
1294 if (!lttng_live_should_quit()) {
1295 fprintf(stderr
, "[error] get_data_packet failed\n");
1299 viewer_stream
->data_pending
= 0;
1301 read_packet_header(pos
, file_stream
);
1307 int lttng_live_create_viewer_session(struct lttng_live_ctx
*ctx
)
1309 struct lttng_viewer_cmd cmd
;
1310 struct lttng_viewer_create_session_response resp
;
1314 if (lttng_live_should_quit()) {
1319 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
1321 cmd
.cmd_version
= 0;
1323 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1325 perror("[error] Error sending cmd");
1328 assert(ret_len
== sizeof(cmd
));
1330 ret_len
= lttng_live_recv(ctx
->control_sock
, &resp
, sizeof(resp
));
1332 fprintf(stderr
, "[error] Remote side has closed connection\n");
1336 perror("[error] Error receiving create session reply");
1339 assert(ret_len
== sizeof(resp
));
1341 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
1342 fprintf(stderr
, "[error] Error creating viewer session\n");
1354 int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
1356 struct bt_context
*bt_ctx
= user_data
;
1357 struct lttng_live_ctf_trace
*trace
= value
;
1360 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
1362 fprintf(stderr
, "[error] removing trace from context\n");
1364 /* remove the key/value pair from the HT. */
1369 int add_one_trace(struct lttng_live_ctx
*ctx
,
1370 struct lttng_live_ctf_trace
*trace
)
1373 struct bt_context
*bt_ctx
= ctx
->bt_ctx
;
1374 struct lttng_live_viewer_stream
*stream
;
1375 struct bt_mmap_stream
*new_mmap_stream
;
1376 struct bt_mmap_stream_list mmap_list
;
1377 struct bt_trace_descriptor
*td
;
1378 struct bt_trace_handle
*handle
;
1381 * We don't know how many streams we will receive for a trace, so
1382 * once we are done receiving the traces, we add all the traces
1383 * received to the bt_context.
1384 * We can receive streams during the attach command or the
1385 * get_new_streams, so we have to make sure not to add multiple
1386 * times the same traces.
1387 * If a trace is already in the context, we just skip this function.
1389 if (trace
->in_use
) {
1394 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1396 for (i
= 0; i
< trace
->streams
->len
; i
++) {
1397 stream
= g_ptr_array_index(trace
->streams
, i
);
1399 if (!stream
->metadata_flag
) {
1400 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
1401 new_mmap_stream
->priv
= (void *) stream
;
1402 new_mmap_stream
->fd
= -1;
1403 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1405 char *metadata_buf
= NULL
;
1407 /* Get all possible metadata before starting */
1408 ret
= get_new_metadata(ctx
, stream
, &metadata_buf
);
1413 if (!stream
->metadata_len
) {
1414 fprintf(stderr
, "[error] empty metadata\n");
1420 trace
->metadata_fp
= babeltrace_fmemopen(metadata_buf
,
1421 stream
->metadata_len
, "rb");
1422 if (!trace
->metadata_fp
) {
1423 perror("Metadata fmemopen");
1431 if (!trace
->metadata_fp
) {
1432 fprintf(stderr
, "[error] No metadata stream opened\n");
1437 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
1438 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
1440 fprintf(stderr
, "[error] Error adding trace\n");
1444 trace
->metadata_stream
->metadata_len
= 0;
1446 handle
= (struct bt_trace_handle
*) g_hash_table_lookup(
1447 bt_ctx
->trace_handles
,
1448 (gpointer
) (unsigned long) ret
);
1450 trace
->handle
= handle
;
1451 if (bt_ctx
->current_iterator
) {
1452 bt_iter_add_trace(bt_ctx
->current_iterator
, td
);
1455 trace
->trace_id
= ret
;
1461 bt_context_put(bt_ctx
);
1467 int add_traces(struct lttng_live_ctx
*ctx
)
1470 struct lttng_live_ctf_trace
*trace
;
1475 g_hash_table_iter_init(&it
, ctx
->session
->ctf_traces
);
1476 while (g_hash_table_iter_next(&it
, &key
, &value
)) {
1477 trace
= (struct lttng_live_ctf_trace
*) value
;
1478 ret
= add_one_trace(ctx
, trace
);
1491 * Request new streams for a session.
1492 * Returns the number of streams received or a negative value on error.
1494 int lttng_live_get_new_streams(struct lttng_live_ctx
*ctx
, uint64_t id
)
1496 struct lttng_viewer_cmd cmd
;
1497 struct lttng_viewer_new_streams_request rq
;
1498 struct lttng_viewer_new_streams_response rp
;
1499 struct lttng_viewer_stream stream
;
1500 int ret
, i
, nb_streams
= 0;
1502 uint32_t stream_count
;
1504 if (lttng_live_should_quit()) {
1509 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1510 cmd
.data_size
= sizeof(rq
);
1511 cmd
.cmd_version
= 0;
1513 memset(&rq
, 0, sizeof(rq
));
1514 rq
.session_id
= htobe64(id
);
1516 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1518 perror("[error] Error sending cmd");
1521 assert(ret_len
== sizeof(cmd
));
1523 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
1525 perror("[error] Error sending get_new_streams request");
1528 assert(ret_len
== sizeof(rq
));
1530 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
1532 fprintf(stderr
, "[error] Remote side has closed connection\n");
1536 perror("[error] Error receiving get_new_streams response");
1539 assert(ret_len
== sizeof(rp
));
1541 switch(be32toh(rp
.status
)) {
1542 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1544 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1547 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1548 ret
= -LTTNG_VIEWER_NEW_STREAMS_HUP
;
1550 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1551 fprintf(stderr
, "[error] get_new_streams error\n");
1554 fprintf(stderr
, "[error] Unknown return code %u\n",
1555 be32toh(rp
.status
));
1559 stream_count
= be32toh(rp
.streams_count
);
1560 ctx
->session
->stream_count
+= stream_count
;
1562 * When the session is created but not started, we do an active wait
1563 * until it starts. It allows the viewer to start processing the trace
1564 * as soon as the session starts.
1566 if (ctx
->session
->stream_count
== 0) {
1570 printf_verbose("Waiting for %" PRIu64
" streams:\n",
1571 ctx
->session
->stream_count
);
1572 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
1573 ctx
->session
->stream_count
);
1574 for (i
= 0; i
< stream_count
; i
++) {
1575 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
, sizeof(stream
));
1577 fprintf(stderr
, "[error] Remote side has closed connection\n");
1581 perror("[error] Error receiving stream");
1584 assert(ret_len
== sizeof(stream
));
1585 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
1586 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
1588 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
1589 be64toh(stream
.id
), stream
.path_name
,
1590 stream
.channel_name
);
1591 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
1592 ctx
->session
->streams
[i
].session
= ctx
->session
;
1594 ctx
->session
->streams
[i
].mmap_size
= 0;
1595 ctx
->session
->streams
[i
].ctf_stream_id
= -1ULL;
1597 if (be32toh(stream
.metadata_flag
)) {
1598 ctx
->session
->streams
[i
].metadata_flag
= 1;
1600 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
1601 be64toh(stream
.ctf_trace_id
));
1616 int lttng_live_read(struct lttng_live_ctx
*ctx
)
1620 struct bt_ctf_iter
*iter
;
1621 const struct bt_ctf_event
*event
;
1622 struct bt_iter_pos begin_pos
;
1623 struct bt_trace_descriptor
*td_write
;
1624 struct bt_format
*fmt_write
;
1625 struct ctf_text_stream_pos
*sout
;
1628 ctx
->bt_ctx
= bt_context_create();
1630 fprintf(stderr
, "[error] bt_context_create allocation\n");
1634 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
1636 fprintf(stderr
, "[error] ctf-text error\n");
1640 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
1642 fprintf(stderr
, "[error] Error opening output trace\n");
1646 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
1648 if (!sout
->parent
.event_cb
) {
1652 ret
= lttng_live_create_viewer_session(ctx
);
1657 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
1658 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
1659 printf_verbose("Attaching to session %" PRIu64
"\n", id
);
1660 ret
= lttng_live_attach_session(ctx
, id
);
1661 printf_verbose("Attaching session returns %d\n", ret
);
1663 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
1664 fprintf(stderr
, "[error] Unknown session ID\n");
1671 * As long as the session is active, we try to get new streams.
1676 if (lttng_live_should_quit()) {
1681 while (!ctx
->session
->stream_count
) {
1682 if (lttng_live_should_quit()
1683 || ctx
->session_ids
->len
== 0) {
1687 ret
= ask_new_streams(ctx
);
1691 if (!ctx
->session
->stream_count
) {
1692 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1696 ret
= add_traces(ctx
);
1701 begin_pos
.type
= BT_SEEK_BEGIN
;
1702 iter
= bt_ctf_iter_create(ctx
->bt_ctx
, &begin_pos
, NULL
);
1704 if (lttng_live_should_quit()) {
1708 fprintf(stderr
, "[error] Iterator creation error\n");
1712 if (lttng_live_should_quit()) {
1716 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
1717 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
1722 ret
= sout
->parent
.event_cb(&sout
->parent
,
1723 event
->parent
->stream
);
1725 fprintf(stderr
, "[error] Writing "
1730 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1735 bt_ctf_iter_destroy(iter
);
1736 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
,
1737 del_traces
, ctx
->bt_ctx
);
1738 ctx
->session
->stream_count
= 0;
1742 bt_context_put(ctx
->bt_ctx
);
1744 if (lttng_live_should_quit()) {