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>
37 #include <babeltrace/ctf/ctf-index.h>
39 #include <babeltrace/babeltrace.h>
40 #include <babeltrace/ctf/events.h>
41 #include <babeltrace/ctf/callbacks.h>
42 #include <babeltrace/ctf/iterator.h>
44 /* for packet_index */
45 #include <babeltrace/ctf/types.h>
47 #include <babeltrace/ctf/metadata.h>
48 #include <babeltrace/ctf-text/types.h>
49 #include <babeltrace/ctf/events-internal.h>
50 #include <formats/ctf/events-private.h>
52 #include "lttng-live-functions.h"
53 #include "lttng-viewer-abi.h"
56 * Memory allocation zeroed
58 #define zmalloc(x) calloc(1, x)
61 #define max_t(type, a, b) \
62 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
65 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
67 static void add_traces(gpointer key
, gpointer value
, gpointer user_data
);
68 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
);
70 int lttng_live_connect_viewer(struct lttng_live_ctx
*ctx
, char *hostname
,
74 struct sockaddr_in server_addr
;
77 host
= gethostbyname(hostname
);
83 if ((ctx
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
89 server_addr
.sin_family
= AF_INET
;
90 server_addr
.sin_port
= htons(port
);
91 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
92 bzero(&(server_addr
.sin_zero
), 8);
94 if (connect(ctx
->control_sock
, (struct sockaddr
*) &server_addr
,
95 sizeof(struct sockaddr
)) == -1) {
107 int lttng_live_establish_connection(struct lttng_live_ctx
*ctx
)
109 struct lttng_viewer_cmd cmd
;
110 struct lttng_viewer_connect connect
;
114 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
115 cmd
.data_size
= sizeof(connect
);
118 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
119 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
120 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
121 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
124 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
125 } while (ret_len
< 0 && errno
== EINTR
);
127 fprintf(stderr
, "[error] Error sending cmd\n");
131 assert(ret_len
== sizeof(cmd
));
134 ret_len
= send(ctx
->control_sock
, &connect
, sizeof(connect
), 0);
135 } while (ret_len
< 0 && errno
== EINTR
);
137 fprintf(stderr
, "[error] Error sending version\n");
141 assert(ret_len
== sizeof(connect
));
144 ret_len
= recv(ctx
->control_sock
, &connect
, sizeof(connect
), 0);
145 } while (ret_len
< 0 && errno
== EINTR
);
147 fprintf(stderr
, "[error] Error receiving version\n");
151 assert(ret_len
== sizeof(connect
));
153 printf_verbose("Received viewer session ID : %" PRIu64
"\n",
154 be64toh(connect
.viewer_session_id
));
155 printf_verbose("Relayd version : %u.%u\n", be32toh(connect
.major
),
156 be32toh(connect
.minor
));
164 int lttng_live_list_sessions(struct lttng_live_ctx
*ctx
, const char *path
)
166 struct lttng_viewer_cmd cmd
;
167 struct lttng_viewer_list_sessions list
;
168 struct lttng_viewer_session lsession
;
173 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
178 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
179 } while (ret_len
< 0 && errno
== EINTR
);
181 fprintf(stderr
, "[error] Error sending cmd\n");
185 assert(ret_len
== sizeof(cmd
));
188 ret_len
= recv(ctx
->control_sock
, &list
, sizeof(list
), 0);
189 } while (ret_len
< 0 && errno
== EINTR
);
191 fprintf(stderr
, "[error] Error receiving session list\n");
195 assert(ret_len
== sizeof(list
));
197 sessions_count
= be32toh(list
.sessions_count
);
198 fprintf(stdout
, "%u active session(s)%c\n", sessions_count
,
199 sessions_count
> 0 ? ':' : ' ');
200 for (i
= 0; i
< sessions_count
; i
++) {
202 ret_len
= recv(ctx
->control_sock
, &lsession
, sizeof(lsession
), 0);
203 } while (ret_len
< 0 && errno
== EINTR
);
205 fprintf(stderr
, "[error] Error receiving session\n");
209 assert(ret_len
== sizeof(lsession
));
210 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
211 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
213 fprintf(stdout
, "%s/%" PRIu64
" : %s on host %s (timer = %u, "
214 "%u stream(s), %u client(s) connected)\n",
215 path
, be64toh(lsession
.id
),
216 lsession
.session_name
, lsession
.hostname
,
217 be32toh(lsession
.live_timer
),
218 be32toh(lsession
.streams
),
219 be32toh(lsession
.clients
));
228 int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream
*stream
,
229 uint64_t ctf_trace_id
)
231 struct lttng_live_ctf_trace
*trace
;
234 trace
= g_hash_table_lookup(stream
->session
->ctf_traces
,
235 (gpointer
) ctf_trace_id
);
237 trace
= g_new0(struct lttng_live_ctf_trace
, 1);
238 trace
->ctf_trace_id
= ctf_trace_id
;
239 trace
->streams
= g_ptr_array_new();
240 g_hash_table_insert(stream
->session
->ctf_traces
,
241 (gpointer
) ctf_trace_id
,
244 if (stream
->metadata_flag
)
245 trace
->metadata_stream
= stream
;
247 stream
->ctf_trace
= trace
;
248 g_ptr_array_add(trace
->streams
, stream
);
253 int lttng_live_attach_session(struct lttng_live_ctx
*ctx
, uint64_t id
)
255 struct lttng_viewer_cmd cmd
;
256 struct lttng_viewer_attach_session_request rq
;
257 struct lttng_viewer_attach_session_response rp
;
258 struct lttng_viewer_stream stream
;
262 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
263 cmd
.data_size
= sizeof(rq
);
266 memset(&rq
, 0, sizeof(rq
));
267 rq
.session_id
= htobe64(id
);
268 // TODO: add cmd line parameter to select seek beginning
269 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
270 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
273 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
274 } while (ret_len
< 0 && errno
== EINTR
);
276 fprintf(stderr
, "[error] Error sending cmd\n");
280 assert(ret_len
== sizeof(cmd
));
283 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
284 } while (ret_len
< 0 && errno
== EINTR
);
286 fprintf(stderr
, "[error] Error sending attach request\n");
290 assert(ret_len
== sizeof(rq
));
293 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
294 } while (ret_len
< 0 && errno
== EINTR
);
296 fprintf(stderr
, "[error] Error receiving attach response\n");
300 assert(ret_len
== sizeof(rp
));
302 switch(be32toh(rp
.status
)) {
303 case LTTNG_VIEWER_ATTACH_OK
:
305 case LTTNG_VIEWER_ATTACH_UNK
:
306 ret
= -LTTNG_VIEWER_ATTACH_UNK
;
308 case LTTNG_VIEWER_ATTACH_ALREADY
:
309 fprintf(stderr
, "[error] Already a viewer attached\n");
312 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
313 fprintf(stderr
, "[error] Not a live session\n");
316 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
317 fprintf(stderr
, "[error] Wrong seek parameter\n");
321 fprintf(stderr
, "[error] Unknown attach return code %u\n",
326 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
331 ctx
->session
->stream_count
+= be32toh(rp
.streams_count
);
333 * When the session is created but not started, we do an active wait
334 * until it starts. It allows the viewer to start processing the trace
335 * as soon as the session starts.
337 if (ctx
->session
->stream_count
== 0) {
341 printf_verbose("Waiting for %" PRIu64
" streams:\n",
342 ctx
->session
->stream_count
);
343 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
344 ctx
->session
->stream_count
);
345 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
347 ret_len
= recv(ctx
->control_sock
, &stream
, sizeof(stream
), 0);
348 } while (ret_len
< 0 && errno
== EINTR
);
350 fprintf(stderr
, "[error] Error receiving stream\n");
354 assert(ret_len
== sizeof(stream
));
355 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
356 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
358 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
359 be64toh(stream
.id
), stream
.path_name
,
360 stream
.channel_name
);
361 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
362 ctx
->session
->streams
[i
].session
= ctx
->session
;
364 ctx
->session
->streams
[i
].first_read
= 1;
365 ctx
->session
->streams
[i
].mmap_size
= 0;
367 if (be32toh(stream
.metadata_flag
)) {
370 path
= strdup(LTTNG_METADATA_PATH_TEMPLATE
);
376 if (!mkdtemp(path
)) {
382 ctx
->session
->streams
[i
].metadata_flag
= 1;
383 snprintf(ctx
->session
->streams
[i
].path
,
384 sizeof(ctx
->session
->streams
[i
].path
),
386 stream
.channel_name
);
387 ret
= open(ctx
->session
->streams
[i
].path
,
388 O_WRONLY
| O_CREAT
| O_TRUNC
,
389 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
395 ctx
->session
->streams
[i
].fd
= ret
;
398 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
399 be64toh(stream
.ctf_trace_id
));
413 int ask_new_streams(struct lttng_live_ctx
*ctx
)
419 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
420 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
421 ret
= lttng_live_get_new_streams(ctx
, id
);
422 printf_verbose("Asking for new streams returns %d\n",
425 if (ret
== -LTTNG_VIEWER_NEW_STREAMS_HUP
) {
426 printf_verbose("Session %" PRIu64
" closed\n",
428 g_array_remove_index(ctx
->session_ids
, i
);
430 * We can't continue iterating on the g_array
431 * after a remove, we have to start again.
446 int get_data_packet(struct lttng_live_ctx
*ctx
,
447 struct ctf_stream_pos
*pos
,
448 struct lttng_live_viewer_stream
*stream
, uint64_t offset
,
451 struct lttng_viewer_cmd cmd
;
452 struct lttng_viewer_get_packet rq
;
453 struct lttng_viewer_trace_packet rp
;
457 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
458 cmd
.data_size
= sizeof(rq
);
461 memset(&rq
, 0, sizeof(rq
));
462 rq
.stream_id
= htobe64(stream
->id
);
463 /* Already in big endian. */
465 rq
.len
= htobe32(len
);
468 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
469 } while (ret_len
< 0 && errno
== EINTR
);
471 fprintf(stderr
, "[error] Error sending cmd\n");
475 assert(ret_len
== sizeof(cmd
));
478 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
479 } while (ret_len
< 0 && errno
== EINTR
);
481 fprintf(stderr
, "[error] Error sending get_data_packet request\n");
485 assert(ret_len
== sizeof(rq
));
488 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
489 } while (ret_len
< 0 && errno
== EINTR
);
491 fprintf(stderr
, "[error] Error receiving data response\n");
495 if (ret_len
!= sizeof(rp
)) {
496 fprintf(stderr
, "[error] get_data_packet: expected %" PRId64
497 ", received %" PRId64
"\n", ret_len
,
503 rp
.flags
= be32toh(rp
.flags
);
505 switch (be32toh(rp
.status
)) {
506 case LTTNG_VIEWER_GET_PACKET_OK
:
507 len
= be32toh(rp
.len
);
508 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
511 case LTTNG_VIEWER_GET_PACKET_RETRY
:
512 printf_verbose("get_data_packet: retry\n");
515 case LTTNG_VIEWER_GET_PACKET_ERR
:
516 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
517 printf_verbose("get_data_packet: new metadata needed\n");
521 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
522 ret
= ask_new_streams(ctx
);
525 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
,
528 fprintf(stderr
, "[error] get_data_packet: error\n");
531 case LTTNG_VIEWER_GET_PACKET_EOF
:
535 printf_verbose("get_data_packet: unknown\n");
546 if (len
> stream
->mmap_size
) {
549 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
552 ret
= munmap_align(pos
->base_mma
);
554 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
559 pos
->base_mma
= NULL
;
561 pos
->base_mma
= mmap_align(new_size
,
562 PROT_READ
| PROT_WRITE
,
563 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
564 if (pos
->base_mma
== MAP_FAILED
) {
565 fprintf(stderr
, "[error] mmap error %s.\n",
567 pos
->base_mma
= NULL
;
572 stream
->mmap_size
= new_size
;
573 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
578 ret_len
= recv(ctx
->control_sock
,
579 mmap_align_addr(pos
->base_mma
), len
,
581 } while (ret_len
< 0 && errno
== EINTR
);
583 fprintf(stderr
, "[error] Error receiving trace packet\n");
587 assert(ret_len
== len
);
595 * Return number of metadata bytes written or a negative value on error.
598 int get_new_metadata(struct lttng_live_ctx
*ctx
,
599 struct lttng_live_viewer_stream
*viewer_stream
,
600 uint64_t *metadata_len
)
604 struct lttng_viewer_cmd cmd
;
605 struct lttng_viewer_get_metadata rq
;
606 struct lttng_viewer_metadata_packet rp
;
607 struct lttng_live_viewer_stream
*metadata_stream
;
611 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
612 cmd
.data_size
= sizeof(rq
);
615 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
616 rq
.stream_id
= htobe64(metadata_stream
->id
);
619 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
620 } while (ret_len
< 0 && errno
== EINTR
);
622 fprintf(stderr
, "[error] Error sending cmd\n");
626 assert(ret_len
== sizeof(cmd
));
629 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
630 } while (ret_len
< 0 && errno
== EINTR
);
632 fprintf(stderr
, "[error] Error sending get_metadata request\n");
636 assert(ret_len
== sizeof(rq
));
639 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
640 } while (ret_len
< 0 && errno
== EINTR
);
642 fprintf(stderr
, "[error] Error receiving metadata response\n");
646 assert(ret_len
== sizeof(rp
));
648 switch (be32toh(rp
.status
)) {
649 case LTTNG_VIEWER_METADATA_OK
:
650 printf_verbose("get_metadata : OK\n");
652 case LTTNG_VIEWER_NO_NEW_METADATA
:
653 printf_verbose("get_metadata : NO NEW\n");
656 case LTTNG_VIEWER_METADATA_ERR
:
657 printf_verbose("get_metadata : ERR\n");
661 printf_verbose("get_metadata : UNKNOWN\n");
666 len
= be64toh(rp
.len
);
667 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
675 perror("relay data zmalloc");
680 ret_len
= recv(ctx
->control_sock
, data
, len
, MSG_WAITALL
);
681 } while (ret_len
< 0 && errno
== EINTR
);
683 fprintf(stderr
, "[error] Error receiving trace packet\n");
688 assert(ret_len
== len
);
691 ret_len
= write(metadata_stream
->fd
, data
, len
);
692 } while (ret_len
< 0 && errno
== EINTR
);
698 assert(ret_len
== len
);
710 * Get one index for a stream.
712 * Returns 0 on success or a negative value on error.
715 int get_next_index(struct lttng_live_ctx
*ctx
,
716 struct lttng_live_viewer_stream
*viewer_stream
,
717 struct packet_index
*index
)
719 struct lttng_viewer_cmd cmd
;
720 struct lttng_viewer_get_next_index rq
;
721 struct lttng_viewer_index rp
;
723 uint64_t metadata_len
;
726 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
727 cmd
.data_size
= sizeof(rq
);
730 memset(&rq
, 0, sizeof(rq
));
731 rq
.stream_id
= htobe64(viewer_stream
->id
);
735 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
736 } while (ret_len
< 0 && errno
== EINTR
);
738 fprintf(stderr
, "[error] Error sending cmd\n");
742 assert(ret_len
== sizeof(cmd
));
745 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
746 } while (ret_len
< 0 && errno
== EINTR
);
748 fprintf(stderr
, "[error] Error sending get_next_index request\n");
752 assert(ret_len
== sizeof(rq
));
755 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
756 } while (ret_len
< 0 && errno
== EINTR
);
758 fprintf(stderr
, "[error] Error receiving index response\n");
762 assert(ret_len
== sizeof(rp
));
764 rp
.flags
= be32toh(rp
.flags
);
766 switch (be32toh(rp
.status
)) {
767 case LTTNG_VIEWER_INDEX_INACTIVE
:
768 printf_verbose("get_next_index: inactive\n");
769 memset(index
, 0, sizeof(struct packet_index
));
770 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
772 case LTTNG_VIEWER_INDEX_OK
:
773 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
774 rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
775 index
->offset
= be64toh(rp
.offset
);
776 index
->packet_size
= be64toh(rp
.packet_size
);
777 index
->content_size
= be64toh(rp
.content_size
);
778 index
->ts_cycles
.timestamp_begin
= be64toh(rp
.timestamp_begin
);
779 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
780 index
->events_discarded
= be64toh(rp
.events_discarded
);
782 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
783 printf_verbose("get_next_index: new metadata needed\n");
784 ret
= get_new_metadata(ctx
, viewer_stream
,
790 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
791 ret
= ask_new_streams(ctx
);
794 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
,
798 case LTTNG_VIEWER_INDEX_RETRY
:
799 printf_verbose("get_next_index: retry\n");
802 case LTTNG_VIEWER_INDEX_HUP
:
803 printf_verbose("get_next_index: stream hung up\n");
804 viewer_stream
->id
= -1ULL;
805 viewer_stream
->fd
= -1;
807 ctx
->session
->stream_count
--;
809 case LTTNG_VIEWER_INDEX_ERR
:
810 fprintf(stderr
, "[error] get_next_index: error\n");
814 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
825 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
828 struct ctf_stream_pos
*pos
;
829 struct ctf_file_stream
*file_stream
;
830 struct packet_index
*prev_index
= NULL
, *cur_index
;
831 struct lttng_live_viewer_stream
*viewer_stream
;
832 struct lttng_live_session
*session
;
836 pos
= ctf_pos(stream_pos
);
837 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
838 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
839 session
= viewer_stream
->session
;
841 switch (pos
->packet_index
->len
) {
843 g_array_set_size(pos
->packet_index
, 1);
844 cur_index
= &g_array_index(pos
->packet_index
,
845 struct packet_index
, 0);
848 g_array_set_size(pos
->packet_index
, 2);
849 prev_index
= &g_array_index(pos
->packet_index
,
850 struct packet_index
, 0);
851 cur_index
= &g_array_index(pos
->packet_index
,
852 struct packet_index
, 1);
855 g_array_index(pos
->packet_index
,
856 struct packet_index
, 0) =
857 g_array_index(pos
->packet_index
,
858 struct packet_index
, 1);
859 prev_index
= &g_array_index(pos
->packet_index
,
860 struct packet_index
, 0);
861 cur_index
= &g_array_index(pos
->packet_index
,
862 struct packet_index
, 1);
868 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
869 ret
= get_next_index(session
->ctx
, viewer_stream
, cur_index
);
872 fprintf(stderr
, "[error] get_next_index failed\n");
876 pos
->packet_size
= cur_index
->packet_size
;
877 pos
->content_size
= cur_index
->content_size
;
878 pos
->mmap_base_offset
= 0;
879 if (cur_index
->offset
== EOF
) {
885 if (cur_index
->content_size
== 0) {
886 file_stream
->parent
.cycles_timestamp
=
887 cur_index
->ts_cycles
.timestamp_end
;
888 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
889 &file_stream
->parent
,
890 cur_index
->ts_cycles
.timestamp_end
);
892 /* Convert the timestamps and append to the real_index. */
893 cur_index
->ts_real
.timestamp_begin
= ctf_get_real_timestamp(
894 &file_stream
->parent
,
895 cur_index
->ts_cycles
.timestamp_begin
);
896 cur_index
->ts_real
.timestamp_end
= ctf_get_real_timestamp(
897 &file_stream
->parent
,
898 cur_index
->ts_cycles
.timestamp_end
);
900 ctf_update_current_packet_index(&file_stream
->parent
,
901 prev_index
, cur_index
);
903 file_stream
->parent
.cycles_timestamp
=
904 cur_index
->ts_cycles
.timestamp_begin
;
905 file_stream
->parent
.real_timestamp
=
906 cur_index
->ts_real
.timestamp_begin
;
909 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
913 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
915 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
916 be64toh(cur_index
->offset
),
917 cur_index
->packet_size
/ CHAR_BIT
);
920 } else if (ret
< 0) {
922 fprintf(stderr
, "[error] get_data_packet failed\n");
926 printf_verbose("Index received : packet_size : %" PRIu64
927 ", offset %" PRIu64
", content_size %" PRIu64
928 ", timestamp_end : %" PRIu64
"\n",
929 cur_index
->packet_size
, cur_index
->offset
,
930 cur_index
->content_size
,
931 cur_index
->ts_cycles
.timestamp_end
);
933 /* update trace_packet_header and stream_packet_context */
934 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
935 /* Read packet header */
936 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
939 fprintf(stderr
, "[error] trace packet header read failed\n");
943 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
944 /* Read packet context */
945 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
948 fprintf(stderr
, "[error] stream packet context read failed\n");
952 pos
->data_offset
= pos
->offset
;
958 int lttng_live_create_viewer_session(struct lttng_live_ctx
*ctx
)
960 struct lttng_viewer_cmd cmd
;
961 struct lttng_viewer_create_session_response resp
;
965 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
970 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
971 } while (ret_len
< 0 && errno
== EINTR
);
973 fprintf(stderr
, "[error] Error sending cmd\n");
977 assert(ret_len
== sizeof(cmd
));
980 ret_len
= recv(ctx
->control_sock
, &resp
, sizeof(resp
), 0);
981 } while (ret_len
< 0 && errno
== EINTR
);
983 fprintf(stderr
, "[error] Error receiving create session reply\n");
987 assert(ret_len
== sizeof(resp
));
989 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
990 fprintf(stderr
, "[error] Error creating viewer session\n");
1001 int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
1003 struct bt_context
*bt_ctx
= user_data
;
1004 struct lttng_live_ctf_trace
*trace
= value
;
1007 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
1009 fprintf(stderr
, "[error] removing trace from context\n");
1011 /* remove the key/value pair from the HT. */
1016 void add_traces(gpointer key
, gpointer value
, gpointer user_data
)
1018 int i
, ret
, total_metadata
= 0;
1019 uint64_t metadata_len
;
1020 struct bt_context
*bt_ctx
= user_data
;
1021 struct lttng_live_ctf_trace
*trace
= value
;
1022 struct lttng_live_viewer_stream
*stream
;
1023 struct bt_mmap_stream
*new_mmap_stream
;
1024 struct bt_mmap_stream_list mmap_list
;
1025 struct lttng_live_ctx
*ctx
= NULL
;
1030 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1032 for (i
= 0; i
< trace
->streams
->len
; i
++) {
1033 stream
= g_ptr_array_index(trace
->streams
, i
);
1034 ctx
= stream
->session
->ctx
;
1036 if (!stream
->metadata_flag
) {
1037 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
1038 new_mmap_stream
->priv
= (void *) stream
;
1039 new_mmap_stream
->fd
= -1;
1040 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1042 /* Get all possible metadata before starting */
1044 ret
= get_new_metadata(ctx
, stream
,
1047 total_metadata
+= metadata_len
;
1049 } while (ret
== 0 || total_metadata
== 0);
1050 trace
->metadata_fp
= fopen(stream
->path
, "r");
1054 if (!trace
->metadata_fp
) {
1055 fprintf(stderr
, "[error] No metadata stream opened\n");
1059 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
1060 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
1062 fprintf(stderr
, "[error] Error adding trace\n");
1066 if (bt_ctx
->current_iterator
) {
1067 struct bt_trace_descriptor
*td
;
1068 struct bt_trace_handle
*handle
;
1070 handle
= (struct bt_trace_handle
*) g_hash_table_lookup(
1071 bt_ctx
->trace_handles
,
1072 (gpointer
) (unsigned long) ret
);
1074 fprintf(stderr
, "Handle : %d, td : %p\n", ret
, td
);
1075 bt_iter_add_trace(bt_ctx
->current_iterator
, td
);
1078 trace
->trace_id
= ret
;
1084 bt_context_put(bt_ctx
);
1089 int lttng_live_get_new_streams(struct lttng_live_ctx
*ctx
, uint64_t id
)
1091 struct lttng_viewer_cmd cmd
;
1092 struct lttng_viewer_new_streams_request rq
;
1093 struct lttng_viewer_new_streams_response rp
;
1094 struct lttng_viewer_stream stream
;
1098 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1099 cmd
.data_size
= sizeof(rq
);
1100 cmd
.cmd_version
= 0;
1102 memset(&rq
, 0, sizeof(rq
));
1103 rq
.session_id
= htobe64(id
);
1106 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
1107 } while (ret_len
< 0 && errno
== EINTR
);
1109 fprintf(stderr
, "[error] Error sending cmd\n");
1113 assert(ret_len
== sizeof(cmd
));
1116 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
1117 } while (ret_len
< 0 && errno
== EINTR
);
1119 fprintf(stderr
, "[error] Error sending attach request\n");
1123 assert(ret_len
== sizeof(rq
));
1126 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
1127 } while (ret_len
< 0 && errno
== EINTR
);
1129 fprintf(stderr
, "[error] Error receiving get_new_streams response\n");
1133 assert(ret_len
== sizeof(rp
));
1135 switch(be32toh(rp
.status
)) {
1136 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1138 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1141 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1142 ret
= -LTTNG_VIEWER_NEW_STREAMS_HUP
;
1144 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1145 fprintf(stderr
, "[error] get_new_streams error\n");
1149 fprintf(stderr
, "[error] Unknown return code %u\n",
1150 be32toh(rp
.status
));
1155 ctx
->session
->stream_count
+= be32toh(rp
.streams_count
);
1157 * When the session is created but not started, we do an active wait
1158 * until it starts. It allows the viewer to start processing the trace
1159 * as soon as the session starts.
1161 if (ctx
->session
->stream_count
== 0) {
1165 printf_verbose("Waiting for %" PRIu64
" streams:\n",
1166 ctx
->session
->stream_count
);
1167 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
1168 ctx
->session
->stream_count
);
1169 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
1171 ret_len
= recv(ctx
->control_sock
, &stream
, sizeof(stream
), 0);
1172 } while (ret_len
< 0 && errno
== EINTR
);
1174 fprintf(stderr
, "[error] Error receiving stream\n");
1178 assert(ret_len
== sizeof(stream
));
1179 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
1180 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
1182 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
1183 be64toh(stream
.id
), stream
.path_name
,
1184 stream
.channel_name
);
1185 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
1186 ctx
->session
->streams
[i
].session
= ctx
->session
;
1188 ctx
->session
->streams
[i
].first_read
= 1;
1189 ctx
->session
->streams
[i
].mmap_size
= 0;
1191 if (be32toh(stream
.metadata_flag
)) {
1194 path
= strdup(LTTNG_METADATA_PATH_TEMPLATE
);
1200 if (!mkdtemp(path
)) {
1206 ctx
->session
->streams
[i
].metadata_flag
= 1;
1207 snprintf(ctx
->session
->streams
[i
].path
,
1208 sizeof(ctx
->session
->streams
[i
].path
),
1210 stream
.channel_name
);
1211 ret
= open(ctx
->session
->streams
[i
].path
,
1212 O_WRONLY
| O_CREAT
| O_TRUNC
,
1213 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1219 ctx
->session
->streams
[i
].fd
= ret
;
1222 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
1223 be64toh(stream
.ctf_trace_id
));
1236 void lttng_live_read(struct lttng_live_ctx
*ctx
)
1239 struct bt_ctf_iter
*iter
;
1240 const struct bt_ctf_event
*event
;
1241 struct bt_iter_pos begin_pos
;
1242 struct bt_trace_descriptor
*td_write
;
1243 struct bt_format
*fmt_write
;
1244 struct ctf_text_stream_pos
*sout
;
1247 ctx
->bt_ctx
= bt_context_create();
1249 fprintf(stderr
, "[error] bt_context_create allocation\n");
1253 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
1255 fprintf(stderr
, "[error] ctf-text error\n");
1259 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
1261 fprintf(stderr
, "[error] Error opening output trace\n");
1265 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
1267 if (!sout
->parent
.event_cb
)
1270 ret
= lttng_live_create_viewer_session(ctx
);
1275 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
1276 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
1277 printf_verbose("Attaching to session %lu\n", id
);
1278 ret
= lttng_live_attach_session(ctx
, id
);
1279 printf_verbose("Attaching session returns %d\n", ret
);
1281 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
1282 fprintf(stderr
, "[error] Unknown session ID\n");
1289 * As long as the session is active, we try to get new streams.
1294 while (!ctx
->session
->stream_count
) {
1295 if (ctx
->session_ids
->len
== 0)
1297 ret
= ask_new_streams(ctx
);
1302 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
,
1305 begin_pos
.type
= BT_SEEK_BEGIN
;
1306 iter
= bt_ctf_iter_create(ctx
->bt_ctx
, &begin_pos
, NULL
);
1308 fprintf(stderr
, "[error] Iterator creation error\n");
1312 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
1313 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
1318 ret
= sout
->parent
.event_cb(&sout
->parent
,
1319 event
->parent
->stream
);
1321 fprintf(stderr
, "[error] Writing "
1326 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1331 bt_ctf_iter_destroy(iter
);
1332 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
,
1333 del_traces
, ctx
->bt_ctx
);
1334 ctx
->session
->stream_count
= 0;
1338 bt_context_put(ctx
->bt_ctx
);