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.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 assert(ret_len
== sizeof(rp
));
459 rp
.flags
= be32toh(rp
.flags
);
461 switch (be32toh(rp
.status
)) {
462 case LTTNG_VIEWER_GET_PACKET_OK
:
463 len
= be32toh(rp
.len
);
464 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
467 case LTTNG_VIEWER_GET_PACKET_RETRY
:
468 printf_verbose("get_data_packet: retry\n");
471 case LTTNG_VIEWER_GET_PACKET_ERR
:
472 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
473 printf_verbose("get_data_packet: new metadata needed\n");
477 fprintf(stderr
, "[error] get_data_packet: error\n");
480 case LTTNG_VIEWER_GET_PACKET_EOF
:
484 printf_verbose("get_data_packet: unknown\n");
495 if (len
> stream
->mmap_size
) {
498 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
501 ret
= munmap_align(pos
->base_mma
);
503 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
508 pos
->base_mma
= NULL
;
510 pos
->base_mma
= mmap_align(new_size
,
511 PROT_READ
| PROT_WRITE
,
512 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
513 if (pos
->base_mma
== MAP_FAILED
) {
514 fprintf(stderr
, "[error] mmap error %s.\n",
516 pos
->base_mma
= NULL
;
521 stream
->mmap_size
= new_size
;
522 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
527 ret_len
= recv(ctx
->control_sock
,
528 mmap_align_addr(pos
->base_mma
), len
,
530 } while (ret_len
< 0 && errno
== EINTR
);
532 fprintf(stderr
, "[error] Error receiving trace packet\n");
536 assert(ret_len
== len
);
544 * Return number of metadata bytes written or a negative value on error.
547 int get_new_metadata(struct lttng_live_ctx
*ctx
,
548 struct lttng_live_viewer_stream
*viewer_stream
,
549 uint64_t *metadata_len
)
553 struct lttng_viewer_cmd cmd
;
554 struct lttng_viewer_get_metadata rq
;
555 struct lttng_viewer_metadata_packet rp
;
556 struct lttng_live_viewer_stream
*metadata_stream
;
560 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
561 cmd
.data_size
= sizeof(rq
);
564 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
565 rq
.stream_id
= htobe64(metadata_stream
->id
);
568 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
569 } while (ret_len
< 0 && errno
== EINTR
);
571 fprintf(stderr
, "[error] Error sending cmd\n");
575 assert(ret_len
== sizeof(cmd
));
578 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
579 } while (ret_len
< 0 && errno
== EINTR
);
581 fprintf(stderr
, "[error] Error sending get_metadata request\n");
585 assert(ret_len
== sizeof(rq
));
588 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
589 } while (ret_len
< 0 && errno
== EINTR
);
591 fprintf(stderr
, "[error] Error receiving metadata response\n");
595 assert(ret_len
== sizeof(rp
));
597 switch (be32toh(rp
.status
)) {
598 case LTTNG_VIEWER_METADATA_OK
:
599 printf_verbose("get_metadata : OK\n");
601 case LTTNG_VIEWER_NO_NEW_METADATA
:
602 printf_verbose("get_metadata : NO NEW\n");
605 case LTTNG_VIEWER_METADATA_ERR
:
606 printf_verbose("get_metadata : ERR\n");
610 printf_verbose("get_metadata : UNKNOWN\n");
615 len
= be64toh(rp
.len
);
616 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
624 perror("relay data zmalloc");
629 ret_len
= recv(ctx
->control_sock
, data
, len
, MSG_WAITALL
);
630 } while (ret_len
< 0 && errno
== EINTR
);
632 fprintf(stderr
, "[error] Error receiving trace packet\n");
637 assert(ret_len
== len
);
640 ret_len
= write(metadata_stream
->fd
, data
, len
);
641 } while (ret_len
< 0 && errno
== EINTR
);
647 assert(ret_len
== len
);
659 * Get one index for a stream.
661 * Returns 0 on success or a negative value on error.
664 int get_next_index(struct lttng_live_ctx
*ctx
,
665 struct lttng_live_viewer_stream
*viewer_stream
,
666 struct packet_index
*index
)
668 struct lttng_viewer_cmd cmd
;
669 struct lttng_viewer_get_next_index rq
;
670 struct lttng_viewer_index rp
;
672 uint64_t metadata_len
;
675 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
676 cmd
.data_size
= sizeof(rq
);
679 memset(&rq
, 0, sizeof(rq
));
680 rq
.stream_id
= htobe64(viewer_stream
->id
);
684 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
685 } while (ret_len
< 0 && errno
== EINTR
);
687 fprintf(stderr
, "[error] Error sending cmd\n");
691 assert(ret_len
== sizeof(cmd
));
694 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
695 } while (ret_len
< 0 && errno
== EINTR
);
697 fprintf(stderr
, "[error] Error sending get_next_index request\n");
701 assert(ret_len
== sizeof(rq
));
704 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
705 } while (ret_len
< 0 && errno
== EINTR
);
707 fprintf(stderr
, "[error] Error receiving index response\n");
711 assert(ret_len
== sizeof(rp
));
713 rp
.flags
= be32toh(rp
.flags
);
715 switch (be32toh(rp
.status
)) {
716 case LTTNG_VIEWER_INDEX_INACTIVE
:
717 printf_verbose("get_next_index: inactive\n");
718 memset(index
, 0, sizeof(struct packet_index
));
719 index
->timestamp_end
= be64toh(rp
.timestamp_end
);
721 case LTTNG_VIEWER_INDEX_OK
:
722 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
723 rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
724 index
->offset
= be64toh(rp
.offset
);
725 index
->packet_size
= be64toh(rp
.packet_size
);
726 index
->content_size
= be64toh(rp
.content_size
);
727 index
->timestamp_begin
= be64toh(rp
.timestamp_begin
);
728 index
->timestamp_end
= be64toh(rp
.timestamp_end
);
729 index
->events_discarded
= be64toh(rp
.events_discarded
);
731 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
732 printf_verbose("get_next_index: new metadata needed\n");
733 ret
= get_new_metadata(ctx
, viewer_stream
,
740 case LTTNG_VIEWER_INDEX_RETRY
:
741 printf_verbose("get_next_index: retry\n");
744 case LTTNG_VIEWER_INDEX_HUP
:
745 printf_verbose("get_next_index: stream hung up\n");
746 viewer_stream
->id
= -1ULL;
747 viewer_stream
->fd
= -1;
750 case LTTNG_VIEWER_INDEX_ERR
:
751 fprintf(stderr
, "[error] get_next_index: error\n");
755 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
766 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
769 struct ctf_stream_pos
*pos
;
770 struct ctf_file_stream
*file_stream
;
771 struct packet_index packet_index
;
772 struct lttng_live_viewer_stream
*viewer_stream
;
773 struct lttng_live_session
*session
;
777 pos
= ctf_pos(stream_pos
);
778 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
779 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
780 session
= viewer_stream
->session
;
782 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
783 ret
= get_next_index(session
->ctx
, viewer_stream
, &packet_index
);
786 fprintf(stderr
, "[error] get_next_index failed\n");
790 pos
->packet_size
= packet_index
.packet_size
;
791 pos
->content_size
= packet_index
.content_size
;
792 pos
->mmap_base_offset
= 0;
793 if (packet_index
.offset
== EOF
) {
799 if (packet_index
.content_size
== 0) {
800 file_stream
->parent
.cycles_timestamp
= packet_index
.timestamp_end
;
801 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
802 &file_stream
->parent
, packet_index
.timestamp_end
);
804 file_stream
->parent
.cycles_timestamp
= packet_index
.timestamp_begin
;
805 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
806 &file_stream
->parent
, packet_index
.timestamp_begin
);
809 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
813 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
815 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
816 be64toh(packet_index
.offset
),
817 packet_index
.packet_size
/ CHAR_BIT
);
820 } else if (ret
< 0) {
822 fprintf(stderr
, "[error] get_data_packet failed\n");
826 printf_verbose("Index received : packet_size : %" PRIu64
827 ", offset %" PRIu64
", content_size %" PRIu64
828 ", timestamp_end : %" PRIu64
"\n",
829 packet_index
.packet_size
, packet_index
.offset
,
830 packet_index
.content_size
, packet_index
.timestamp_end
);
832 /* update trace_packet_header and stream_packet_context */
833 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
834 /* Read packet header */
835 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
838 fprintf(stderr
, "[error] trace packet header read failed\n");
842 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
843 /* Read packet context */
844 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
847 fprintf(stderr
, "[error] stream packet context read failed\n");
851 pos
->data_offset
= pos
->offset
;
857 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
859 struct bt_context
*bt_ctx
= user_data
;
860 struct lttng_live_ctf_trace
*trace
= value
;
863 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
865 fprintf(stderr
, "[error] removing trace from context\n");
867 /* remove the key/value pair from the HT. */
871 static void add_traces(gpointer key
, gpointer value
, gpointer user_data
)
873 int i
, ret
, total_metadata
= 0;
874 uint64_t metadata_len
;
875 struct bt_context
*bt_ctx
= user_data
;
876 struct lttng_live_ctf_trace
*trace
= value
;
877 struct lttng_live_viewer_stream
*stream
;
878 struct bt_mmap_stream
*new_mmap_stream
;
879 struct bt_mmap_stream_list mmap_list
;
880 struct lttng_live_ctx
*ctx
= NULL
;
882 BT_INIT_LIST_HEAD(&mmap_list
.head
);
884 for (i
= 0; i
< trace
->streams
->len
; i
++) {
885 stream
= g_ptr_array_index(trace
->streams
, i
);
886 ctx
= stream
->session
->ctx
;
888 if (!stream
->metadata_flag
) {
889 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
890 new_mmap_stream
->priv
= (void *) stream
;
891 new_mmap_stream
->fd
= -1;
892 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
894 /* Get all possible metadata before starting */
896 ret
= get_new_metadata(ctx
, stream
,
899 total_metadata
+= metadata_len
;
901 } while (ret
== 0 || total_metadata
== 0);
902 trace
->metadata_fp
= fopen(stream
->path
, "r");
906 if (!trace
->metadata_fp
) {
907 fprintf(stderr
, "[error] No metadata stream opened\n");
911 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
912 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
914 fprintf(stderr
, "[error] Error adding trace\n");
917 trace
->trace_id
= ret
;
922 bt_context_put(bt_ctx
);
927 void lttng_live_read(struct lttng_live_ctx
*ctx
, uint64_t session_id
)
929 int ret
, active_session
= 0;
930 struct bt_context
*bt_ctx
;
931 struct bt_ctf_iter
*iter
;
932 const struct bt_ctf_event
*event
;
933 struct bt_iter_pos begin_pos
;
934 struct bt_trace_descriptor
*td_write
;
935 struct bt_format
*fmt_write
;
936 struct ctf_text_stream_pos
*sout
;
938 bt_ctx
= bt_context_create();
940 fprintf(stderr
, "[error] bt_context_create allocation\n");
944 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
946 fprintf(stderr
, "[error] ctf-text error\n");
950 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
952 fprintf(stderr
, "[error] Error opening output trace\n");
956 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
958 if (!sout
->parent
.event_cb
)
962 * As long as the session is active, we try to reattach to it,
963 * even if all the streams get closed.
969 ret
= lttng_live_attach_session(ctx
, session_id
);
970 printf_verbose("Attaching session returns %d\n", ret
);
972 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
975 fprintf(stderr
, "[error] Unknown "
982 } while (ctx
->session
->stream_count
== 0);
984 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
, bt_ctx
);
986 begin_pos
.type
= BT_SEEK_BEGIN
;
987 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
989 fprintf(stderr
, "[error] Iterator creation error\n");
993 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
994 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
999 ret
= sout
->parent
.event_cb(&sout
->parent
,
1000 event
->parent
->stream
);
1002 fprintf(stderr
, "[error] Writing "
1007 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1012 bt_ctf_iter_destroy(iter
);
1013 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
, del_traces
, bt_ctx
);
1014 } while (active_session
);
1017 bt_context_put(bt_ctx
);