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 void add_traces(gpointer key
, gpointer value
, gpointer user_data
);
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
, NAME_MAX
) == 0) &&
267 strncmp(relay_session
->name
,
268 session_name
, NAME_MAX
) == 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
, NAME_MAX
);
281 relay_session
->name
= strndup(session_name
, NAME_MAX
);
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 NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
356 ctx
->traced_hostname
, NAME_MAX
) == 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
,
385 (gpointer
) ctf_trace_id
);
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 (gpointer
) 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 %" PRId64
685 ", received %" PRId64
"\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
);
715 g_hash_table_foreach(ctx
->session
->ctf_traces
,
716 add_traces
, ctx
->bt_ctx
);
718 if (rp
.flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
719 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
722 fprintf(stderr
, "[error] get_data_packet: error\n");
724 case LTTNG_VIEWER_GET_PACKET_EOF
:
728 printf_verbose("get_data_packet: unknown\n");
736 if (len
> stream
->mmap_size
) {
739 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
742 ret
= munmap_align(pos
->base_mma
);
744 perror("[error] Unable to unmap old base");
747 pos
->base_mma
= NULL
;
749 pos
->base_mma
= mmap_align(new_size
,
750 PROT_READ
| PROT_WRITE
,
751 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
752 if (pos
->base_mma
== MAP_FAILED
) {
753 perror("[error] mmap error");
754 pos
->base_mma
= NULL
;
758 stream
->mmap_size
= new_size
;
759 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
763 ret_len
= lttng_live_recv(ctx
->control_sock
,
764 mmap_align_addr(pos
->base_mma
), len
);
766 fprintf(stderr
, "[error] Remote side has closed connection\n");
770 perror("[error] Error receiving trace packet");
773 assert(ret_len
== len
);
783 int get_one_metadata_packet(struct lttng_live_ctx
*ctx
,
784 struct lttng_live_viewer_stream
*metadata_stream
)
788 struct lttng_viewer_cmd cmd
;
789 struct lttng_viewer_get_metadata rq
;
790 struct lttng_viewer_metadata_packet rp
;
794 if (lttng_live_should_quit()) {
799 rq
.stream_id
= htobe64(metadata_stream
->id
);
800 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
801 cmd
.data_size
= sizeof(rq
);
804 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
806 perror("[error] Error sending cmd");
809 assert(ret_len
== sizeof(cmd
));
811 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
813 perror("[error] Error sending get_metadata request");
816 assert(ret_len
== sizeof(rq
));
818 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
820 fprintf(stderr
, "[error] Remote side has closed connection\n");
824 perror("[error] Error receiving metadata response");
827 assert(ret_len
== sizeof(rp
));
829 switch (be32toh(rp
.status
)) {
830 case LTTNG_VIEWER_METADATA_OK
:
831 printf_verbose("get_metadata : OK\n");
833 case LTTNG_VIEWER_NO_NEW_METADATA
:
834 printf_verbose("get_metadata : NO NEW\n");
837 case LTTNG_VIEWER_METADATA_ERR
:
838 printf_verbose("get_metadata : ERR\n");
841 printf_verbose("get_metadata : UNKNOWN\n");
845 len
= be64toh(rp
.len
);
846 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
853 perror("relay data zmalloc");
856 ret_len
= lttng_live_recv(ctx
->control_sock
, data
, len
);
858 fprintf(stderr
, "[error] Remote side has closed connection\n");
859 goto error_free_data
;
862 perror("[error] Error receiving trace packet");
863 goto error_free_data
;
865 assert(ret_len
== len
);
868 ret_len
= fwrite(data
, 1, len
,
869 metadata_stream
->metadata_fp_write
);
870 } while (ret_len
< 0 && errno
== EINTR
);
872 fprintf(stderr
, "[error] Writing in the metadata fp\n");
873 goto error_free_data
;
875 assert(ret_len
== len
);
876 metadata_stream
->metadata_len
+= len
;
889 * Return 0 on success, a negative value on error.
892 int get_new_metadata(struct lttng_live_ctx
*ctx
,
893 struct lttng_live_viewer_stream
*viewer_stream
,
897 struct lttng_live_viewer_stream
*metadata_stream
;
898 size_t size
, len_read
= 0;
900 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
901 if (!metadata_stream
) {
902 fprintf(stderr
, "[error] No metadata stream\n");
906 metadata_stream
->metadata_len
= 0;
907 ret
= open_metadata_fp_write(metadata_stream
, metadata_buf
, &size
);
914 * get_one_metadata_packet returns the number of bytes
915 * received, 0 when we have received everything, a
916 * negative value on error.
918 ret
= get_one_metadata_packet(ctx
, metadata_stream
);
923 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
925 } while (ret
> 0 || !len_read
);
927 if (fclose(metadata_stream
->metadata_fp_write
))
929 metadata_stream
->metadata_fp_write
= NULL
;
936 * Assign the fields from a lttng_viewer_index to a packet_index.
939 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
940 struct packet_index
*pindex
)
945 pindex
->offset
= be64toh(lindex
->offset
);
946 pindex
->packet_size
= be64toh(lindex
->packet_size
);
947 pindex
->content_size
= be64toh(lindex
->content_size
);
948 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
949 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
950 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
954 * Get one index for a stream.
956 * Returns 0 on success or a negative value on error.
959 int get_next_index(struct lttng_live_ctx
*ctx
,
960 struct lttng_live_viewer_stream
*viewer_stream
,
961 struct packet_index
*index
, uint64_t *stream_id
)
963 struct lttng_viewer_cmd cmd
;
964 struct lttng_viewer_get_next_index rq
;
967 struct lttng_viewer_index
*rp
= &viewer_stream
->current_index
;
969 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
970 cmd
.data_size
= sizeof(rq
);
973 memset(&rq
, 0, sizeof(rq
));
974 rq
.stream_id
= htobe64(viewer_stream
->id
);
977 if (lttng_live_should_quit()) {
981 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
983 perror("[error] Error sending cmd");
986 assert(ret_len
== sizeof(cmd
));
988 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
990 perror("[error] Error sending get_next_index request");
993 assert(ret_len
== sizeof(rq
));
995 ret_len
= lttng_live_recv(ctx
->control_sock
, rp
, sizeof(*rp
));
997 fprintf(stderr
, "[error] Remote side has closed connection\n");
1001 perror("[error] Error receiving index response");
1004 assert(ret_len
== sizeof(*rp
));
1006 rp
->flags
= be32toh(rp
->flags
);
1008 switch (be32toh(rp
->status
)) {
1009 case LTTNG_VIEWER_INDEX_INACTIVE
:
1010 printf_verbose("get_next_index: inactive\n");
1011 memset(index
, 0, sizeof(struct packet_index
));
1012 index
->ts_cycles
.timestamp_end
= be64toh(rp
->timestamp_end
);
1013 *stream_id
= be64toh(rp
->stream_id
);
1015 case LTTNG_VIEWER_INDEX_OK
:
1016 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
1017 rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
1018 lttng_index_to_packet_index(rp
, index
);
1019 *stream_id
= be64toh(rp
->stream_id
);
1020 viewer_stream
->data_pending
= 1;
1022 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1023 ret
= append_metadata(ctx
, viewer_stream
);
1027 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1028 printf_verbose("get_next_index: need new streams\n");
1029 ret
= ask_new_streams(ctx
);
1033 g_hash_table_foreach(ctx
->session
->ctf_traces
,
1034 add_traces
, ctx
->bt_ctx
);
1037 case LTTNG_VIEWER_INDEX_RETRY
:
1038 printf_verbose("get_next_index: retry\n");
1039 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1041 case LTTNG_VIEWER_INDEX_HUP
:
1042 printf_verbose("get_next_index: stream hung up\n");
1043 viewer_stream
->id
= -1ULL;
1044 index
->offset
= EOF
;
1045 ctx
->session
->stream_count
--;
1047 case LTTNG_VIEWER_INDEX_ERR
:
1048 fprintf(stderr
, "[error] get_next_index: error\n");
1051 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
1063 void read_packet_header(struct ctf_stream_pos
*pos
,
1064 struct ctf_file_stream
*file_stream
)
1068 /* update trace_packet_header and stream_packet_context */
1069 if (!(pos
->prot
& PROT_WRITE
) &&
1070 file_stream
->parent
.trace_packet_header
) {
1071 /* Read packet header */
1072 ret
= generic_rw(&pos
->parent
,
1073 &file_stream
->parent
.trace_packet_header
->p
);
1076 fprintf(stderr
, "[error] trace packet "
1077 "header read failed\n");
1081 if (!(pos
->prot
& PROT_WRITE
) &&
1082 file_stream
->parent
.stream_packet_context
) {
1083 /* Read packet context */
1084 ret
= generic_rw(&pos
->parent
,
1085 &file_stream
->parent
.stream_packet_context
->p
);
1088 fprintf(stderr
, "[error] stream packet "
1089 "context read failed\n");
1093 pos
->data_offset
= pos
->offset
;
1100 * Handle the seek parameters.
1101 * Returns 0 if the packet_seek can continue, a positive value to
1102 * cleanly exit the packet_seek, a negative value on error.
1105 int handle_seek_position(size_t index
, int whence
,
1106 struct lttng_live_viewer_stream
*viewer_stream
,
1107 struct ctf_stream_pos
*pos
,
1108 struct ctf_file_stream
*file_stream
)
1118 * We only allow to seek to 0.
1121 fprintf(stderr
, "[error] Arbitrary seek in lttng-live "
1122 "trace not supported\n");
1132 fprintf(stderr
, "[error] Invalid seek parameter\n");
1141 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
1144 struct ctf_stream_pos
*pos
;
1145 struct ctf_file_stream
*file_stream
;
1146 struct packet_index
*prev_index
= NULL
, *cur_index
;
1147 struct lttng_live_viewer_stream
*viewer_stream
;
1148 struct lttng_live_session
*session
;
1149 uint64_t stream_id
= -1ULL;
1152 pos
= ctf_pos(stream_pos
);
1153 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
1154 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
1155 session
= viewer_stream
->session
;
1157 ret
= handle_seek_position(index
, whence
, viewer_stream
, pos
,
1164 switch (pos
->packet_index
->len
) {
1166 g_array_set_size(pos
->packet_index
, 1);
1167 cur_index
= &g_array_index(pos
->packet_index
,
1168 struct packet_index
, 0);
1171 g_array_set_size(pos
->packet_index
, 2);
1172 prev_index
= &g_array_index(pos
->packet_index
,
1173 struct packet_index
, 0);
1174 cur_index
= &g_array_index(pos
->packet_index
,
1175 struct packet_index
, 1);
1178 g_array_index(pos
->packet_index
,
1179 struct packet_index
, 0) =
1180 g_array_index(pos
->packet_index
,
1181 struct packet_index
, 1);
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);
1192 if (viewer_stream
->data_pending
) {
1193 lttng_index_to_packet_index(&viewer_stream
->current_index
, cur_index
);
1195 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
1196 ret
= get_next_index(session
->ctx
, viewer_stream
, cur_index
, &stream_id
);
1199 if (!lttng_live_should_quit()) {
1200 fprintf(stderr
, "[error] get_next_index failed\n");
1204 printf_verbose("Index received : packet_size : %" PRIu64
1205 ", offset %" PRIu64
", content_size %" PRIu64
1206 ", timestamp_end : %" PRIu64
"\n",
1207 cur_index
->packet_size
, cur_index
->offset
,
1208 cur_index
->content_size
,
1209 cur_index
->ts_cycles
.timestamp_end
);
1214 * On the first time we receive an index, the stream_id needs to
1215 * be set for the stream in order to use it, we don't want any
1216 * data at this stage.
1218 if (file_stream
->parent
.stream_id
== -1ULL) {
1220 * Warning: with lttng-tools < 2.4.2, the beacon does not
1221 * contain the real stream ID, it is memset to 0, so this
1222 * might create a problem when a session has multiple
1223 * channels. We can't detect it at this stage, lttng-tools
1224 * has to be upgraded to fix this problem.
1226 printf_verbose("Assigning stream_id %" PRIu64
"\n",
1228 file_stream
->parent
.stream_id
= stream_id
;
1229 viewer_stream
->ctf_stream_id
= stream_id
;
1234 pos
->packet_size
= cur_index
->packet_size
;
1235 pos
->content_size
= cur_index
->content_size
;
1236 pos
->mmap_base_offset
= 0;
1237 if (cur_index
->offset
== EOF
) {
1243 if (cur_index
->content_size
== 0) {
1244 if (file_stream
->parent
.stream_class
) {
1245 file_stream
->parent
.cycles_timestamp
=
1246 cur_index
->ts_cycles
.timestamp_end
;
1247 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
1248 &file_stream
->parent
,
1249 cur_index
->ts_cycles
.timestamp_end
);
1252 if (file_stream
->parent
.stream_class
) {
1253 /* Convert the timestamps and append to the real_index. */
1254 cur_index
->ts_real
.timestamp_begin
= ctf_get_real_timestamp(
1255 &file_stream
->parent
,
1256 cur_index
->ts_cycles
.timestamp_begin
);
1257 cur_index
->ts_real
.timestamp_end
= ctf_get_real_timestamp(
1258 &file_stream
->parent
,
1259 cur_index
->ts_cycles
.timestamp_end
);
1262 ctf_update_current_packet_index(&file_stream
->parent
,
1263 prev_index
, cur_index
);
1265 file_stream
->parent
.cycles_timestamp
=
1266 cur_index
->ts_cycles
.timestamp_begin
;
1267 file_stream
->parent
.real_timestamp
=
1268 cur_index
->ts_real
.timestamp_begin
;
1271 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
1275 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
1277 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
1279 cur_index
->packet_size
/ CHAR_BIT
);
1282 } else if (ret
< 0) {
1284 if (!lttng_live_should_quit()) {
1285 fprintf(stderr
, "[error] get_data_packet failed\n");
1289 viewer_stream
->data_pending
= 0;
1291 read_packet_header(pos
, file_stream
);
1297 int lttng_live_create_viewer_session(struct lttng_live_ctx
*ctx
)
1299 struct lttng_viewer_cmd cmd
;
1300 struct lttng_viewer_create_session_response resp
;
1304 if (lttng_live_should_quit()) {
1309 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
1311 cmd
.cmd_version
= 0;
1313 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1315 perror("[error] Error sending cmd");
1318 assert(ret_len
== sizeof(cmd
));
1320 ret_len
= lttng_live_recv(ctx
->control_sock
, &resp
, sizeof(resp
));
1322 fprintf(stderr
, "[error] Remote side has closed connection\n");
1326 perror("[error] Error receiving create session reply");
1329 assert(ret_len
== sizeof(resp
));
1331 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
1332 fprintf(stderr
, "[error] Error creating viewer session\n");
1344 int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
1346 struct bt_context
*bt_ctx
= user_data
;
1347 struct lttng_live_ctf_trace
*trace
= value
;
1350 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
1352 fprintf(stderr
, "[error] removing trace from context\n");
1354 /* remove the key/value pair from the HT. */
1359 void add_traces(gpointer key
, gpointer value
, gpointer user_data
)
1362 struct bt_context
*bt_ctx
= user_data
;
1363 struct lttng_live_ctf_trace
*trace
= value
;
1364 struct lttng_live_viewer_stream
*stream
;
1365 struct bt_mmap_stream
*new_mmap_stream
;
1366 struct bt_mmap_stream_list mmap_list
;
1367 struct lttng_live_ctx
*ctx
= NULL
;
1368 struct bt_trace_descriptor
*td
;
1369 struct bt_trace_handle
*handle
;
1372 * We don't know how many streams we will receive for a trace, so
1373 * once we are done receiving the traces, we add all the traces
1374 * received to the bt_context.
1375 * We can receive streams during the attach command or the
1376 * get_new_streams, so we have to make sure not to add multiple
1377 * times the same traces.
1378 * If a trace is already in the context, we just skip this function.
1383 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1385 for (i
= 0; i
< trace
->streams
->len
; i
++) {
1386 stream
= g_ptr_array_index(trace
->streams
, i
);
1387 ctx
= stream
->session
->ctx
;
1389 if (!stream
->metadata_flag
) {
1390 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
1391 new_mmap_stream
->priv
= (void *) stream
;
1392 new_mmap_stream
->fd
= -1;
1393 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1395 char *metadata_buf
= NULL
;
1397 /* Get all possible metadata before starting */
1398 ret
= get_new_metadata(ctx
, stream
, &metadata_buf
);
1403 if (!stream
->metadata_len
) {
1404 fprintf(stderr
, "[error] empty metadata\n");
1410 trace
->metadata_fp
= babeltrace_fmemopen(metadata_buf
,
1411 stream
->metadata_len
, "rb");
1412 if (!trace
->metadata_fp
) {
1413 perror("Metadata fmemopen");
1421 if (!trace
->metadata_fp
) {
1422 fprintf(stderr
, "[error] No metadata stream opened\n");
1426 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
1427 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
1429 fprintf(stderr
, "[error] Error adding trace\n");
1432 trace
->metadata_stream
->metadata_len
= 0;
1434 handle
= (struct bt_trace_handle
*) g_hash_table_lookup(
1435 bt_ctx
->trace_handles
,
1436 (gpointer
) (unsigned long) ret
);
1438 trace
->handle
= handle
;
1439 if (bt_ctx
->current_iterator
) {
1440 bt_iter_add_trace(bt_ctx
->current_iterator
, td
);
1443 trace
->trace_id
= ret
;
1449 bt_context_put(bt_ctx
);
1455 * Request new streams for a session.
1456 * Returns the number of streams received or a negative value on error.
1458 int lttng_live_get_new_streams(struct lttng_live_ctx
*ctx
, uint64_t id
)
1460 struct lttng_viewer_cmd cmd
;
1461 struct lttng_viewer_new_streams_request rq
;
1462 struct lttng_viewer_new_streams_response rp
;
1463 struct lttng_viewer_stream stream
;
1464 int ret
, i
, nb_streams
= 0;
1466 uint32_t stream_count
;
1468 if (lttng_live_should_quit()) {
1473 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1474 cmd
.data_size
= sizeof(rq
);
1475 cmd
.cmd_version
= 0;
1477 memset(&rq
, 0, sizeof(rq
));
1478 rq
.session_id
= htobe64(id
);
1480 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1482 perror("[error] Error sending cmd");
1485 assert(ret_len
== sizeof(cmd
));
1487 ret_len
= lttng_live_send(ctx
->control_sock
, &rq
, sizeof(rq
));
1489 perror("[error] Error sending get_new_streams request");
1492 assert(ret_len
== sizeof(rq
));
1494 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
1496 fprintf(stderr
, "[error] Remote side has closed connection\n");
1500 perror("[error] Error receiving get_new_streams response");
1503 assert(ret_len
== sizeof(rp
));
1505 switch(be32toh(rp
.status
)) {
1506 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1508 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1511 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1512 ret
= -LTTNG_VIEWER_NEW_STREAMS_HUP
;
1514 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1515 fprintf(stderr
, "[error] get_new_streams error\n");
1518 fprintf(stderr
, "[error] Unknown return code %u\n",
1519 be32toh(rp
.status
));
1523 stream_count
= be32toh(rp
.streams_count
);
1524 ctx
->session
->stream_count
+= stream_count
;
1526 * When the session is created but not started, we do an active wait
1527 * until it starts. It allows the viewer to start processing the trace
1528 * as soon as the session starts.
1530 if (ctx
->session
->stream_count
== 0) {
1534 printf_verbose("Waiting for %" PRIu64
" streams:\n",
1535 ctx
->session
->stream_count
);
1536 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
1537 ctx
->session
->stream_count
);
1538 for (i
= 0; i
< stream_count
; i
++) {
1539 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
, sizeof(stream
));
1541 fprintf(stderr
, "[error] Remote side has closed connection\n");
1545 perror("[error] Error receiving stream");
1548 assert(ret_len
== sizeof(stream
));
1549 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
1550 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
1552 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
1553 be64toh(stream
.id
), stream
.path_name
,
1554 stream
.channel_name
);
1555 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
1556 ctx
->session
->streams
[i
].session
= ctx
->session
;
1558 ctx
->session
->streams
[i
].mmap_size
= 0;
1559 ctx
->session
->streams
[i
].ctf_stream_id
= -1ULL;
1561 if (be32toh(stream
.metadata_flag
)) {
1562 ctx
->session
->streams
[i
].metadata_flag
= 1;
1564 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
1565 be64toh(stream
.ctf_trace_id
));
1580 int lttng_live_read(struct lttng_live_ctx
*ctx
)
1584 struct bt_ctf_iter
*iter
;
1585 const struct bt_ctf_event
*event
;
1586 struct bt_iter_pos begin_pos
;
1587 struct bt_trace_descriptor
*td_write
;
1588 struct bt_format
*fmt_write
;
1589 struct ctf_text_stream_pos
*sout
;
1592 ctx
->bt_ctx
= bt_context_create();
1594 fprintf(stderr
, "[error] bt_context_create allocation\n");
1598 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
1600 fprintf(stderr
, "[error] ctf-text error\n");
1604 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
1606 fprintf(stderr
, "[error] Error opening output trace\n");
1610 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
1612 if (!sout
->parent
.event_cb
) {
1616 ret
= lttng_live_create_viewer_session(ctx
);
1621 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
1622 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
1623 printf_verbose("Attaching to session %lu\n", id
);
1624 ret
= lttng_live_attach_session(ctx
, id
);
1625 printf_verbose("Attaching session returns %d\n", ret
);
1627 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
1628 fprintf(stderr
, "[error] Unknown session ID\n");
1635 * As long as the session is active, we try to get new streams.
1640 if (lttng_live_should_quit()) {
1645 while (!ctx
->session
->stream_count
) {
1646 if (lttng_live_should_quit()
1647 || ctx
->session_ids
->len
== 0) {
1651 ret
= ask_new_streams(ctx
);
1655 if (!ctx
->session
->stream_count
) {
1656 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1660 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
,
1663 begin_pos
.type
= BT_SEEK_BEGIN
;
1664 iter
= bt_ctf_iter_create(ctx
->bt_ctx
, &begin_pos
, NULL
);
1666 if (lttng_live_should_quit()) {
1670 fprintf(stderr
, "[error] Iterator creation error\n");
1674 if (lttng_live_should_quit()) {
1678 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
1679 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
1684 ret
= sout
->parent
.event_cb(&sout
->parent
,
1685 event
->parent
->stream
);
1687 fprintf(stderr
, "[error] Writing "
1692 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1697 bt_ctf_iter_destroy(iter
);
1698 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
,
1699 del_traces
, ctx
->bt_ctx
);
1700 ctx
->session
->stream_count
= 0;
1704 bt_context_put(ctx
->bt_ctx
);
1706 if (lttng_live_should_quit()) {