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 int lttng_live_connect_viewer(struct lttng_live_ctx
*ctx
, char *hostname
,
69 struct sockaddr_in server_addr
;
72 host
= gethostbyname(hostname
);
78 if ((ctx
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
84 server_addr
.sin_family
= AF_INET
;
85 server_addr
.sin_port
= htons(port
);
86 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
87 bzero(&(server_addr
.sin_zero
), 8);
89 if (connect(ctx
->control_sock
, (struct sockaddr
*) &server_addr
,
90 sizeof(struct sockaddr
)) == -1) {
102 int lttng_live_establish_connection(struct lttng_live_ctx
*ctx
)
104 struct lttng_viewer_cmd cmd
;
105 struct lttng_viewer_connect connect
;
109 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
110 cmd
.data_size
= sizeof(connect
);
113 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
114 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
115 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
116 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
119 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
120 } while (ret_len
< 0 && errno
== EINTR
);
122 fprintf(stderr
, "[error] Error sending cmd\n");
126 assert(ret_len
== sizeof(cmd
));
129 ret_len
= send(ctx
->control_sock
, &connect
, sizeof(connect
), 0);
130 } while (ret_len
< 0 && errno
== EINTR
);
132 fprintf(stderr
, "[error] Error sending version\n");
136 assert(ret_len
== sizeof(connect
));
139 ret_len
= recv(ctx
->control_sock
, &connect
, sizeof(connect
), 0);
140 } while (ret_len
< 0 && errno
== EINTR
);
142 fprintf(stderr
, "[error] Error receiving version\n");
146 assert(ret_len
== sizeof(connect
));
148 printf_verbose("Received viewer session ID : %" PRIu64
"\n",
149 be64toh(connect
.viewer_session_id
));
150 printf_verbose("Relayd version : %u.%u\n", be32toh(connect
.major
),
151 be32toh(connect
.minor
));
159 int lttng_live_list_sessions(struct lttng_live_ctx
*ctx
, const char *path
)
161 struct lttng_viewer_cmd cmd
;
162 struct lttng_viewer_list_sessions list
;
163 struct lttng_viewer_session lsession
;
168 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
173 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
174 } while (ret_len
< 0 && errno
== EINTR
);
176 fprintf(stderr
, "[error] Error sending cmd\n");
180 assert(ret_len
== sizeof(cmd
));
183 ret_len
= recv(ctx
->control_sock
, &list
, sizeof(list
), 0);
184 } while (ret_len
< 0 && errno
== EINTR
);
186 fprintf(stderr
, "[error] Error receiving session list\n");
190 assert(ret_len
== sizeof(list
));
192 sessions_count
= be32toh(list
.sessions_count
);
193 fprintf(stdout
, "%u active session(s)%c\n", sessions_count
,
194 sessions_count
> 0 ? ':' : ' ');
195 for (i
= 0; i
< sessions_count
; i
++) {
197 ret_len
= recv(ctx
->control_sock
, &lsession
, sizeof(lsession
), 0);
198 } while (ret_len
< 0 && errno
== EINTR
);
200 fprintf(stderr
, "[error] Error receiving session\n");
204 assert(ret_len
== sizeof(lsession
));
205 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
206 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
208 fprintf(stdout
, "%s/%" PRIu64
" : %s on host %s (timer = %u, "
209 "%u stream(s), %u client(s) connected)\n",
210 path
, be64toh(lsession
.id
),
211 lsession
.session_name
, lsession
.hostname
,
212 be32toh(lsession
.live_timer
),
213 be32toh(lsession
.streams
),
214 be32toh(lsession
.clients
));
223 int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream
*stream
,
224 uint64_t ctf_trace_id
)
226 struct lttng_live_ctf_trace
*trace
;
229 trace
= g_hash_table_lookup(stream
->session
->ctf_traces
,
230 (gpointer
) ctf_trace_id
);
232 trace
= g_new0(struct lttng_live_ctf_trace
, 1);
233 trace
->ctf_trace_id
= ctf_trace_id
;
234 trace
->streams
= g_ptr_array_new();
235 g_hash_table_insert(stream
->session
->ctf_traces
,
236 (gpointer
) ctf_trace_id
,
239 if (stream
->metadata_flag
)
240 trace
->metadata_stream
= stream
;
242 stream
->ctf_trace
= trace
;
243 g_ptr_array_add(trace
->streams
, stream
);
248 int lttng_live_attach_session(struct lttng_live_ctx
*ctx
, uint64_t id
)
250 struct lttng_viewer_cmd cmd
;
251 struct lttng_viewer_attach_session_request rq
;
252 struct lttng_viewer_attach_session_response rp
;
253 struct lttng_viewer_stream stream
;
257 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
258 cmd
.data_size
= sizeof(rq
);
261 memset(&rq
, 0, sizeof(rq
));
262 rq
.session_id
= htobe64(id
);
263 // TODO: add cmd line parameter to select seek beginning
264 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
265 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
268 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
269 } while (ret_len
< 0 && errno
== EINTR
);
271 fprintf(stderr
, "[error] Error sending cmd\n");
275 assert(ret_len
== sizeof(cmd
));
278 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
279 } while (ret_len
< 0 && errno
== EINTR
);
281 fprintf(stderr
, "[error] Error sending attach request\n");
285 assert(ret_len
== sizeof(rq
));
288 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
289 } while (ret_len
< 0 && errno
== EINTR
);
291 fprintf(stderr
, "[error] Error receiving attach response\n");
295 assert(ret_len
== sizeof(rp
));
297 switch(be32toh(rp
.status
)) {
298 case LTTNG_VIEWER_ATTACH_OK
:
300 case LTTNG_VIEWER_ATTACH_UNK
:
301 ret
= -LTTNG_VIEWER_ATTACH_UNK
;
303 case LTTNG_VIEWER_ATTACH_ALREADY
:
304 fprintf(stderr
, "[error] Already a viewer attached\n");
307 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
308 fprintf(stderr
, "[error] Not a live session\n");
311 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
312 fprintf(stderr
, "[error] Wrong seek parameter\n");
316 fprintf(stderr
, "[error] Unknown attach return code %u\n",
321 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
326 ctx
->session
->stream_count
= be32toh(rp
.streams_count
);
328 * When the session is created but not started, we do an active wait
329 * until it starts. It allows the viewer to start processing the trace
330 * as soon as the session starts.
332 if (ctx
->session
->stream_count
== 0) {
336 printf_verbose("Waiting for %" PRIu64
" streams:\n",
337 ctx
->session
->stream_count
);
338 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
339 ctx
->session
->stream_count
);
340 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
342 ret_len
= recv(ctx
->control_sock
, &stream
, sizeof(stream
), 0);
343 } while (ret_len
< 0 && errno
== EINTR
);
345 fprintf(stderr
, "[error] Error receiving stream\n");
349 assert(ret_len
== sizeof(stream
));
350 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
351 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
353 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
354 be64toh(stream
.id
), stream
.path_name
,
355 stream
.channel_name
);
356 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
357 ctx
->session
->streams
[i
].session
= ctx
->session
;
359 ctx
->session
->streams
[i
].first_read
= 1;
360 ctx
->session
->streams
[i
].mmap_size
= 0;
362 if (be32toh(stream
.metadata_flag
)) {
365 path
= strdup(LTTNG_METADATA_PATH_TEMPLATE
);
371 if (!mkdtemp(path
)) {
377 ctx
->session
->streams
[i
].metadata_flag
= 1;
378 snprintf(ctx
->session
->streams
[i
].path
,
379 sizeof(ctx
->session
->streams
[i
].path
),
381 stream
.channel_name
);
382 ret
= open(ctx
->session
->streams
[i
].path
,
383 O_WRONLY
| O_CREAT
| O_TRUNC
,
384 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
390 ctx
->session
->streams
[i
].fd
= ret
;
393 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
394 be64toh(stream
.ctf_trace_id
));
408 int get_data_packet(struct lttng_live_ctx
*ctx
,
409 struct ctf_stream_pos
*pos
,
410 struct lttng_live_viewer_stream
*stream
, uint64_t offset
,
413 struct lttng_viewer_cmd cmd
;
414 struct lttng_viewer_get_packet rq
;
415 struct lttng_viewer_trace_packet rp
;
419 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
420 cmd
.data_size
= sizeof(rq
);
423 memset(&rq
, 0, sizeof(rq
));
424 rq
.stream_id
= htobe64(stream
->id
);
425 /* Already in big endian. */
427 rq
.len
= htobe32(len
);
430 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
431 } while (ret_len
< 0 && errno
== EINTR
);
433 fprintf(stderr
, "[error] Error sending cmd\n");
437 assert(ret_len
== sizeof(cmd
));
440 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
441 } while (ret_len
< 0 && errno
== EINTR
);
443 fprintf(stderr
, "[error] Error sending get_data_packet request\n");
447 assert(ret_len
== sizeof(rq
));
450 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
451 } while (ret_len
< 0 && errno
== EINTR
);
453 fprintf(stderr
, "[error] Error receiving data response\n");
457 if (ret_len
!= sizeof(rp
)) {
458 fprintf(stderr
, "[error] get_data_packet: expected %" PRId64
459 ", received %" PRId64
"\n", ret_len
,
465 rp
.flags
= be32toh(rp
.flags
);
467 switch (be32toh(rp
.status
)) {
468 case LTTNG_VIEWER_GET_PACKET_OK
:
469 len
= be32toh(rp
.len
);
470 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
473 case LTTNG_VIEWER_GET_PACKET_RETRY
:
474 printf_verbose("get_data_packet: retry\n");
477 case LTTNG_VIEWER_GET_PACKET_ERR
:
478 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
479 printf_verbose("get_data_packet: new metadata needed\n");
483 fprintf(stderr
, "[error] get_data_packet: error\n");
486 case LTTNG_VIEWER_GET_PACKET_EOF
:
490 printf_verbose("get_data_packet: unknown\n");
501 if (len
> stream
->mmap_size
) {
504 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
507 ret
= munmap_align(pos
->base_mma
);
509 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
514 pos
->base_mma
= NULL
;
516 pos
->base_mma
= mmap_align(new_size
,
517 PROT_READ
| PROT_WRITE
,
518 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
519 if (pos
->base_mma
== MAP_FAILED
) {
520 fprintf(stderr
, "[error] mmap error %s.\n",
522 pos
->base_mma
= NULL
;
527 stream
->mmap_size
= new_size
;
528 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
533 ret_len
= recv(ctx
->control_sock
,
534 mmap_align_addr(pos
->base_mma
), len
,
536 } while (ret_len
< 0 && errno
== EINTR
);
538 fprintf(stderr
, "[error] Error receiving trace packet\n");
542 assert(ret_len
== len
);
550 * Return number of metadata bytes written or a negative value on error.
553 int get_new_metadata(struct lttng_live_ctx
*ctx
,
554 struct lttng_live_viewer_stream
*viewer_stream
,
555 uint64_t *metadata_len
)
559 struct lttng_viewer_cmd cmd
;
560 struct lttng_viewer_get_metadata rq
;
561 struct lttng_viewer_metadata_packet rp
;
562 struct lttng_live_viewer_stream
*metadata_stream
;
566 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
567 cmd
.data_size
= sizeof(rq
);
570 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
571 rq
.stream_id
= htobe64(metadata_stream
->id
);
574 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
575 } while (ret_len
< 0 && errno
== EINTR
);
577 fprintf(stderr
, "[error] Error sending cmd\n");
581 assert(ret_len
== sizeof(cmd
));
584 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
585 } while (ret_len
< 0 && errno
== EINTR
);
587 fprintf(stderr
, "[error] Error sending get_metadata request\n");
591 assert(ret_len
== sizeof(rq
));
594 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
595 } while (ret_len
< 0 && errno
== EINTR
);
597 fprintf(stderr
, "[error] Error receiving metadata response\n");
601 assert(ret_len
== sizeof(rp
));
603 switch (be32toh(rp
.status
)) {
604 case LTTNG_VIEWER_METADATA_OK
:
605 printf_verbose("get_metadata : OK\n");
607 case LTTNG_VIEWER_NO_NEW_METADATA
:
608 printf_verbose("get_metadata : NO NEW\n");
611 case LTTNG_VIEWER_METADATA_ERR
:
612 printf_verbose("get_metadata : ERR\n");
616 printf_verbose("get_metadata : UNKNOWN\n");
621 len
= be64toh(rp
.len
);
622 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
630 perror("relay data zmalloc");
635 ret_len
= recv(ctx
->control_sock
, data
, len
, MSG_WAITALL
);
636 } while (ret_len
< 0 && errno
== EINTR
);
638 fprintf(stderr
, "[error] Error receiving trace packet\n");
643 assert(ret_len
== len
);
646 ret_len
= write(metadata_stream
->fd
, data
, len
);
647 } while (ret_len
< 0 && errno
== EINTR
);
653 assert(ret_len
== len
);
665 * Get one index for a stream.
667 * Returns 0 on success or a negative value on error.
670 int get_next_index(struct lttng_live_ctx
*ctx
,
671 struct lttng_live_viewer_stream
*viewer_stream
,
672 struct packet_index
*index
)
674 struct lttng_viewer_cmd cmd
;
675 struct lttng_viewer_get_next_index rq
;
676 struct lttng_viewer_index rp
;
678 uint64_t metadata_len
;
681 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
682 cmd
.data_size
= sizeof(rq
);
685 memset(&rq
, 0, sizeof(rq
));
686 rq
.stream_id
= htobe64(viewer_stream
->id
);
690 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
691 } while (ret_len
< 0 && errno
== EINTR
);
693 fprintf(stderr
, "[error] Error sending cmd\n");
697 assert(ret_len
== sizeof(cmd
));
700 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
701 } while (ret_len
< 0 && errno
== EINTR
);
703 fprintf(stderr
, "[error] Error sending get_next_index request\n");
707 assert(ret_len
== sizeof(rq
));
710 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
711 } while (ret_len
< 0 && errno
== EINTR
);
713 fprintf(stderr
, "[error] Error receiving index response\n");
717 assert(ret_len
== sizeof(rp
));
719 rp
.flags
= be32toh(rp
.flags
);
721 switch (be32toh(rp
.status
)) {
722 case LTTNG_VIEWER_INDEX_INACTIVE
:
723 printf_verbose("get_next_index: inactive\n");
724 memset(index
, 0, sizeof(struct packet_index
));
725 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
727 case LTTNG_VIEWER_INDEX_OK
:
728 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
729 rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
730 index
->offset
= be64toh(rp
.offset
);
731 index
->packet_size
= be64toh(rp
.packet_size
);
732 index
->content_size
= be64toh(rp
.content_size
);
733 index
->ts_cycles
.timestamp_begin
= be64toh(rp
.timestamp_begin
);
734 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
735 index
->events_discarded
= be64toh(rp
.events_discarded
);
737 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
738 printf_verbose("get_next_index: new metadata needed\n");
739 ret
= get_new_metadata(ctx
, viewer_stream
,
746 case LTTNG_VIEWER_INDEX_RETRY
:
747 printf_verbose("get_next_index: retry\n");
750 case LTTNG_VIEWER_INDEX_HUP
:
751 printf_verbose("get_next_index: stream hung up\n");
752 viewer_stream
->id
= -1ULL;
753 viewer_stream
->fd
= -1;
756 case LTTNG_VIEWER_INDEX_ERR
:
757 fprintf(stderr
, "[error] get_next_index: error\n");
761 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
772 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
775 struct ctf_stream_pos
*pos
;
776 struct ctf_file_stream
*file_stream
;
777 struct packet_index
*prev_index
= NULL
, *cur_index
;
778 struct lttng_live_viewer_stream
*viewer_stream
;
779 struct lttng_live_session
*session
;
783 pos
= ctf_pos(stream_pos
);
784 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
785 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
786 session
= viewer_stream
->session
;
788 switch (pos
->packet_index
->len
) {
790 g_array_set_size(pos
->packet_index
, 1);
791 cur_index
= &g_array_index(pos
->packet_index
,
792 struct packet_index
, 0);
795 g_array_set_size(pos
->packet_index
, 2);
796 prev_index
= &g_array_index(pos
->packet_index
,
797 struct packet_index
, 0);
798 cur_index
= &g_array_index(pos
->packet_index
,
799 struct packet_index
, 1);
802 g_array_index(pos
->packet_index
,
803 struct packet_index
, 0) =
804 g_array_index(pos
->packet_index
,
805 struct packet_index
, 1);
806 prev_index
= &g_array_index(pos
->packet_index
,
807 struct packet_index
, 0);
808 cur_index
= &g_array_index(pos
->packet_index
,
809 struct packet_index
, 1);
815 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
816 ret
= get_next_index(session
->ctx
, viewer_stream
, cur_index
);
819 fprintf(stderr
, "[error] get_next_index failed\n");
823 pos
->packet_size
= cur_index
->packet_size
;
824 pos
->content_size
= cur_index
->content_size
;
825 pos
->mmap_base_offset
= 0;
826 if (cur_index
->offset
== EOF
) {
832 if (cur_index
->content_size
== 0) {
833 file_stream
->parent
.cycles_timestamp
=
834 cur_index
->ts_cycles
.timestamp_end
;
835 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
836 &file_stream
->parent
,
837 cur_index
->ts_cycles
.timestamp_end
);
839 /* Convert the timestamps and append to the real_index. */
840 cur_index
->ts_real
.timestamp_begin
= ctf_get_real_timestamp(
841 &file_stream
->parent
,
842 cur_index
->ts_cycles
.timestamp_begin
);
843 cur_index
->ts_real
.timestamp_end
= ctf_get_real_timestamp(
844 &file_stream
->parent
,
845 cur_index
->ts_cycles
.timestamp_end
);
847 ctf_update_current_packet_index(&file_stream
->parent
,
848 prev_index
, cur_index
);
850 file_stream
->parent
.cycles_timestamp
=
851 cur_index
->ts_cycles
.timestamp_begin
;
852 file_stream
->parent
.real_timestamp
=
853 cur_index
->ts_real
.timestamp_begin
;
856 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
860 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
862 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
863 be64toh(cur_index
->offset
),
864 cur_index
->packet_size
/ CHAR_BIT
);
867 } else if (ret
< 0) {
869 fprintf(stderr
, "[error] get_data_packet failed\n");
873 printf_verbose("Index received : packet_size : %" PRIu64
874 ", offset %" PRIu64
", content_size %" PRIu64
875 ", timestamp_end : %" PRIu64
"\n",
876 cur_index
->packet_size
, cur_index
->offset
,
877 cur_index
->content_size
,
878 cur_index
->ts_cycles
.timestamp_end
);
880 /* update trace_packet_header and stream_packet_context */
881 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
882 /* Read packet header */
883 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
886 fprintf(stderr
, "[error] trace packet header read failed\n");
890 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
891 /* Read packet context */
892 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
895 fprintf(stderr
, "[error] stream packet context read failed\n");
899 pos
->data_offset
= pos
->offset
;
905 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
907 struct bt_context
*bt_ctx
= user_data
;
908 struct lttng_live_ctf_trace
*trace
= value
;
911 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
913 fprintf(stderr
, "[error] removing trace from context\n");
915 /* remove the key/value pair from the HT. */
919 static void add_traces(gpointer key
, gpointer value
, gpointer user_data
)
921 int i
, ret
, total_metadata
= 0;
922 uint64_t metadata_len
;
923 struct bt_context
*bt_ctx
= user_data
;
924 struct lttng_live_ctf_trace
*trace
= value
;
925 struct lttng_live_viewer_stream
*stream
;
926 struct bt_mmap_stream
*new_mmap_stream
;
927 struct bt_mmap_stream_list mmap_list
;
928 struct lttng_live_ctx
*ctx
= NULL
;
930 BT_INIT_LIST_HEAD(&mmap_list
.head
);
932 for (i
= 0; i
< trace
->streams
->len
; i
++) {
933 stream
= g_ptr_array_index(trace
->streams
, i
);
934 ctx
= stream
->session
->ctx
;
936 if (!stream
->metadata_flag
) {
937 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
938 new_mmap_stream
->priv
= (void *) stream
;
939 new_mmap_stream
->fd
= -1;
940 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
942 /* Get all possible metadata before starting */
944 ret
= get_new_metadata(ctx
, stream
,
947 total_metadata
+= metadata_len
;
949 } while (ret
== 0 || total_metadata
== 0);
950 trace
->metadata_fp
= fopen(stream
->path
, "r");
954 if (!trace
->metadata_fp
) {
955 fprintf(stderr
, "[error] No metadata stream opened\n");
959 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
960 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
962 fprintf(stderr
, "[error] Error adding trace\n");
965 trace
->trace_id
= ret
;
970 bt_context_put(bt_ctx
);
975 int lttng_live_create_viewer_session(struct lttng_live_ctx
*ctx
)
977 struct lttng_viewer_cmd cmd
;
978 struct lttng_viewer_create_session_response resp
;
982 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
987 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
988 } while (ret_len
< 0 && errno
== EINTR
);
990 fprintf(stderr
, "[error] Error sending cmd\n");
994 assert(ret_len
== sizeof(cmd
));
997 ret_len
= recv(ctx
->control_sock
, &resp
, sizeof(resp
), 0);
998 } while (ret_len
< 0 && errno
== EINTR
);
1000 fprintf(stderr
, "[error] Error receiving create session reply\n");
1004 assert(ret_len
== sizeof(resp
));
1006 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
1007 fprintf(stderr
, "[error] Error creating viewer session\n");
1017 void lttng_live_read(struct lttng_live_ctx
*ctx
, uint64_t session_id
)
1019 int ret
, active_session
= 0;
1020 struct bt_context
*bt_ctx
;
1021 struct bt_ctf_iter
*iter
;
1022 const struct bt_ctf_event
*event
;
1023 struct bt_iter_pos begin_pos
;
1024 struct bt_trace_descriptor
*td_write
;
1025 struct bt_format
*fmt_write
;
1026 struct ctf_text_stream_pos
*sout
;
1028 bt_ctx
= bt_context_create();
1030 fprintf(stderr
, "[error] bt_context_create allocation\n");
1034 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
1036 fprintf(stderr
, "[error] ctf-text error\n");
1040 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
1042 fprintf(stderr
, "[error] Error opening output trace\n");
1046 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
1048 if (!sout
->parent
.event_cb
)
1051 ret
= lttng_live_create_viewer_session(ctx
);
1057 * As long as the session is active, we try to reattach to it,
1058 * even if all the streams get closed.
1064 ret
= lttng_live_attach_session(ctx
, session_id
);
1065 printf_verbose("Attaching session returns %d\n", ret
);
1067 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
1070 fprintf(stderr
, "[error] Unknown "
1077 } while (ctx
->session
->stream_count
== 0);
1079 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
, bt_ctx
);
1081 begin_pos
.type
= BT_SEEK_BEGIN
;
1082 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
1084 fprintf(stderr
, "[error] Iterator creation error\n");
1088 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
1089 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
1094 ret
= sout
->parent
.event_cb(&sout
->parent
,
1095 event
->parent
->stream
);
1097 fprintf(stderr
, "[error] Writing "
1102 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1107 bt_ctf_iter_destroy(iter
);
1108 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
, del_traces
, bt_ctx
);
1109 } while (active_session
);
1112 bt_context_put(bt_ctx
);