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
.major
= htobe32(LTTNG_LIVE_MAJOR
);
114 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
115 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
118 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
119 } while (ret_len
< 0 && errno
== EINTR
);
121 fprintf(stderr
, "[error] Error sending cmd\n");
125 assert(ret_len
== sizeof(cmd
));
128 ret_len
= send(ctx
->control_sock
, &connect
, sizeof(connect
), 0);
129 } while (ret_len
< 0 && errno
== EINTR
);
131 fprintf(stderr
, "[error] Error sending version\n");
135 assert(ret_len
== sizeof(connect
));
138 ret_len
= recv(ctx
->control_sock
, &connect
, sizeof(connect
), 0);
139 } while (ret_len
< 0 && errno
== EINTR
);
141 fprintf(stderr
, "[error] Error receiving version\n");
145 assert(ret_len
== sizeof(connect
));
147 printf_verbose("Received viewer session ID : %" PRIu64
"\n",
148 be64toh(connect
.viewer_session_id
));
149 printf_verbose("Relayd version : %u.%u\n", be32toh(connect
.major
),
150 be32toh(connect
.minor
));
158 int lttng_live_list_sessions(struct lttng_live_ctx
*ctx
, const char *path
)
160 struct lttng_viewer_cmd cmd
;
161 struct lttng_viewer_list_sessions list
;
162 struct lttng_viewer_session lsession
;
167 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
172 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
173 } while (ret_len
< 0 && errno
== EINTR
);
175 fprintf(stderr
, "[error] Error sending cmd\n");
179 assert(ret_len
== sizeof(cmd
));
182 ret_len
= recv(ctx
->control_sock
, &list
, sizeof(list
), 0);
183 } while (ret_len
< 0 && errno
== EINTR
);
185 fprintf(stderr
, "[error] Error receiving session list\n");
189 assert(ret_len
== sizeof(list
));
191 sessions_count
= be32toh(list
.sessions_count
);
192 fprintf(stdout
, "%u active session(s)%c\n", sessions_count
,
193 sessions_count
> 0 ? ':' : ' ');
194 for (i
= 0; i
< sessions_count
; i
++) {
196 ret_len
= recv(ctx
->control_sock
, &lsession
, sizeof(lsession
), 0);
197 } while (ret_len
< 0 && errno
== EINTR
);
199 fprintf(stderr
, "[error] Error receiving session\n");
203 assert(ret_len
== sizeof(lsession
));
205 fprintf(stdout
, "%s/%" PRIu64
" : %s on host %s (timer = %u, "
206 "%u stream(s), %u client(s) connected)\n",
207 path
, be64toh(lsession
.id
),
208 lsession
.session_name
, lsession
.hostname
,
209 be32toh(lsession
.live_timer
),
210 be32toh(lsession
.streams
),
211 be32toh(lsession
.clients
));
220 int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream
*stream
,
221 uint64_t ctf_trace_id
)
223 struct lttng_live_ctf_trace
*trace
;
226 trace
= g_hash_table_lookup(stream
->session
->ctf_traces
,
227 (gpointer
) ctf_trace_id
);
229 trace
= g_new0(struct lttng_live_ctf_trace
, 1);
232 fprintf(stderr
, "[error] ctf_trace allocation\n");
235 trace
->ctf_trace_id
= ctf_trace_id
;
236 trace
->streams
= g_ptr_array_new();
237 g_hash_table_insert(stream
->session
->ctf_traces
,
238 (gpointer
) ctf_trace_id
,
241 if (stream
->metadata_flag
)
242 trace
->metadata_stream
= stream
;
244 stream
->ctf_trace
= trace
;
245 g_ptr_array_add(trace
->streams
, stream
);
251 int lttng_live_attach_session(struct lttng_live_ctx
*ctx
, uint64_t id
)
253 struct lttng_viewer_cmd cmd
;
254 struct lttng_viewer_attach_session_request rq
;
255 struct lttng_viewer_attach_session_response rp
;
256 struct lttng_viewer_stream stream
;
260 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
261 cmd
.data_size
= sizeof(rq
);
264 memset(&rq
, 0, sizeof(rq
));
265 rq
.session_id
= htobe64(id
);
266 // TODO: add cmd line parameter to select seek beginning
267 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
268 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
271 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
272 } while (ret_len
< 0 && errno
== EINTR
);
274 fprintf(stderr
, "[error] Error sending cmd\n");
278 assert(ret_len
== sizeof(cmd
));
281 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
282 } while (ret_len
< 0 && errno
== EINTR
);
284 fprintf(stderr
, "[error] Error sending attach request\n");
288 assert(ret_len
== sizeof(rq
));
291 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
292 } while (ret_len
< 0 && errno
== EINTR
);
294 fprintf(stderr
, "[error] Error receiving attach response\n");
298 assert(ret_len
== sizeof(rp
));
300 switch(be32toh(rp
.status
)) {
301 case LTTNG_VIEWER_ATTACH_OK
:
303 case LTTNG_VIEWER_ATTACH_UNK
:
304 ret
= -LTTNG_VIEWER_ATTACH_UNK
;
306 case LTTNG_VIEWER_ATTACH_ALREADY
:
307 fprintf(stderr
, "[error] Already a viewer attached\n");
310 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
311 fprintf(stderr
, "[error] Not a live session\n");
314 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
315 fprintf(stderr
, "[error] Wrong seek parameter\n");
319 fprintf(stderr
, "[error] Unknown attach return code %u\n",
324 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
329 ctx
->session
->stream_count
= be32toh(rp
.streams_count
);
331 * When the session is created but not started, we do an active wait
332 * until it starts. It allows the viewer to start processing the trace
333 * as soon as the session starts.
335 if (ctx
->session
->stream_count
== 0) {
339 printf_verbose("Waiting for %" PRIu64
" streams:\n",
340 ctx
->session
->stream_count
);
341 ctx
->session
->streams
= g_new0(struct lttng_live_viewer_stream
,
342 ctx
->session
->stream_count
);
343 if (!ctx
->session
->streams
) {
348 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
350 ret_len
= recv(ctx
->control_sock
, &stream
, sizeof(stream
), 0);
351 } while (ret_len
< 0 && errno
== EINTR
);
353 fprintf(stderr
, "[error] Error receiving stream\n");
357 assert(ret_len
== sizeof(stream
));
359 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
360 be64toh(stream
.id
), stream
.path_name
,
361 stream
.channel_name
);
362 ctx
->session
->streams
[i
].id
= be64toh(stream
.id
);
363 ctx
->session
->streams
[i
].session
= ctx
->session
;
365 ctx
->session
->streams
[i
].first_read
= 1;
366 ctx
->session
->streams
[i
].mmap_size
= 0;
368 if (be32toh(stream
.metadata_flag
)) {
371 path
= strdup(LTTNG_METADATA_PATH_TEMPLATE
);
372 path
= mkdtemp(path
);
373 ctx
->session
->streams
[i
].metadata_flag
= 1;
374 snprintf(ctx
->session
->streams
[i
].path
,
375 sizeof(ctx
->session
->streams
[i
].path
),
377 stream
.channel_name
);
378 ret
= open(ctx
->session
->streams
[i
].path
,
379 O_WRONLY
| O_CREAT
| O_TRUNC
,
380 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
385 ctx
->session
->streams
[i
].fd
= ret
;
387 ret
= lttng_live_ctf_trace_assign(&ctx
->session
->streams
[i
],
388 be64toh(stream
.ctf_trace_id
));
402 int get_data_packet(struct lttng_live_ctx
*ctx
,
403 struct ctf_stream_pos
*pos
,
404 struct lttng_live_viewer_stream
*stream
, uint64_t offset
,
407 struct lttng_viewer_cmd cmd
;
408 struct lttng_viewer_get_packet rq
;
409 struct lttng_viewer_trace_packet rp
;
413 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
414 cmd
.data_size
= sizeof(rq
);
417 memset(&rq
, 0, sizeof(rq
));
418 rq
.stream_id
= htobe64(stream
->id
);
419 /* Already in big endian. */
421 rq
.len
= htobe32(len
);
424 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
425 } while (ret_len
< 0 && errno
== EINTR
);
427 fprintf(stderr
, "[error] Error sending cmd\n");
431 assert(ret_len
== sizeof(cmd
));
434 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
435 } while (ret_len
< 0 && errno
== EINTR
);
437 fprintf(stderr
, "[error] Error sending get_data_packet request\n");
441 assert(ret_len
== sizeof(rq
));
444 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
445 } while (ret_len
< 0 && errno
== EINTR
);
447 fprintf(stderr
, "[error] Error receiving data response\n");
451 assert(ret_len
== sizeof(rp
));
453 rp
.flags
= be32toh(rp
.flags
);
455 switch (be32toh(rp
.status
)) {
456 case LTTNG_VIEWER_GET_PACKET_OK
:
457 len
= be32toh(rp
.len
);
458 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
461 case LTTNG_VIEWER_GET_PACKET_RETRY
:
462 printf_verbose("get_data_packet: retry\n");
465 case LTTNG_VIEWER_GET_PACKET_ERR
:
466 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
467 printf_verbose("get_data_packet: new metadata needed\n");
471 fprintf(stderr
, "[error] get_data_packet: error\n");
474 case LTTNG_VIEWER_GET_PACKET_EOF
:
478 printf_verbose("get_data_packet: unknown\n");
489 if (len
> stream
->mmap_size
) {
492 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
495 ret
= munmap_align(pos
->base_mma
);
497 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
502 pos
->base_mma
= NULL
;
504 pos
->base_mma
= mmap_align(new_size
,
505 PROT_READ
| PROT_WRITE
,
506 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
507 if (pos
->base_mma
== MAP_FAILED
) {
508 fprintf(stderr
, "[error] mmap error %s.\n",
510 pos
->base_mma
= NULL
;
515 stream
->mmap_size
= new_size
;
516 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
521 ret_len
= recv(ctx
->control_sock
,
522 mmap_align_addr(pos
->base_mma
), len
,
524 } while (ret_len
< 0 && errno
== EINTR
);
526 fprintf(stderr
, "[error] Error receiving trace packet\n");
530 assert(ret_len
== len
);
538 * Return number of metadata bytes written or a negative value on error.
541 int get_new_metadata(struct lttng_live_ctx
*ctx
,
542 struct lttng_live_viewer_stream
*viewer_stream
,
543 uint64_t *metadata_len
)
547 struct lttng_viewer_cmd cmd
;
548 struct lttng_viewer_get_metadata rq
;
549 struct lttng_viewer_metadata_packet rp
;
550 struct lttng_live_viewer_stream
*metadata_stream
;
554 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
555 cmd
.data_size
= sizeof(rq
);
558 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
559 rq
.stream_id
= htobe64(metadata_stream
->id
);
562 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
563 } while (ret_len
< 0 && errno
== EINTR
);
565 fprintf(stderr
, "[error] Error sending cmd\n");
569 assert(ret_len
== sizeof(cmd
));
572 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
573 } while (ret_len
< 0 && errno
== EINTR
);
575 fprintf(stderr
, "[error] Error sending get_metadata request\n");
579 assert(ret_len
== sizeof(rq
));
582 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
583 } while (ret_len
< 0 && errno
== EINTR
);
585 fprintf(stderr
, "[error] Error receiving metadata response\n");
589 assert(ret_len
== sizeof(rp
));
591 switch (be32toh(rp
.status
)) {
592 case LTTNG_VIEWER_METADATA_OK
:
593 printf_verbose("get_metadata : OK\n");
595 case LTTNG_VIEWER_NO_NEW_METADATA
:
596 printf_verbose("get_metadata : NO NEW\n");
599 case LTTNG_VIEWER_METADATA_ERR
:
600 printf_verbose("get_metadata : ERR\n");
604 printf_verbose("get_metadata : UNKNOWN\n");
609 len
= be64toh(rp
.len
);
610 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
618 perror("relay data zmalloc");
623 ret_len
= recv(ctx
->control_sock
, data
, len
, MSG_WAITALL
);
624 } while (ret_len
< 0 && errno
== EINTR
);
626 fprintf(stderr
, "[error] Error receiving trace packet\n");
631 assert(ret_len
== len
);
634 ret_len
= write(metadata_stream
->fd
, data
, len
);
635 } while (ret_len
< 0 && errno
== EINTR
);
641 assert(ret_len
== len
);
653 * Get one index for a stream.
655 * Returns 0 on success or a negative value on error.
658 int get_next_index(struct lttng_live_ctx
*ctx
,
659 struct lttng_live_viewer_stream
*viewer_stream
,
660 struct packet_index
*index
)
662 struct lttng_viewer_cmd cmd
;
663 struct lttng_viewer_get_next_index rq
;
664 struct lttng_viewer_index rp
;
666 uint64_t metadata_len
;
669 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
670 cmd
.data_size
= sizeof(rq
);
673 memset(&rq
, 0, sizeof(rq
));
674 rq
.stream_id
= htobe64(viewer_stream
->id
);
678 ret_len
= send(ctx
->control_sock
, &cmd
, sizeof(cmd
), 0);
679 } while (ret_len
< 0 && errno
== EINTR
);
681 fprintf(stderr
, "[error] Error sending cmd\n");
685 assert(ret_len
== sizeof(cmd
));
688 ret_len
= send(ctx
->control_sock
, &rq
, sizeof(rq
), 0);
689 } while (ret_len
< 0 && errno
== EINTR
);
691 fprintf(stderr
, "[error] Error sending get_next_index request\n");
695 assert(ret_len
== sizeof(rq
));
698 ret_len
= recv(ctx
->control_sock
, &rp
, sizeof(rp
), 0);
699 } while (ret_len
< 0 && errno
== EINTR
);
701 fprintf(stderr
, "[error] Error receiving index response\n");
705 assert(ret_len
== sizeof(rp
));
707 rp
.flags
= be32toh(rp
.flags
);
709 switch (be32toh(rp
.status
)) {
710 case LTTNG_VIEWER_INDEX_INACTIVE
:
711 printf_verbose("get_next_index: inactive\n");
712 memset(index
, 0, sizeof(struct packet_index
));
713 index
->timestamp_end
= be64toh(rp
.timestamp_end
);
715 case LTTNG_VIEWER_INDEX_OK
:
716 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
717 rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
718 index
->offset
= be64toh(rp
.offset
);
719 index
->packet_size
= be64toh(rp
.packet_size
);
720 index
->content_size
= be64toh(rp
.content_size
);
721 index
->timestamp_begin
= be64toh(rp
.timestamp_begin
);
722 index
->timestamp_end
= be64toh(rp
.timestamp_end
);
723 index
->events_discarded
= be64toh(rp
.events_discarded
);
725 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
726 printf_verbose("get_next_index: new metadata needed\n");
727 ret
= get_new_metadata(ctx
, viewer_stream
,
734 case LTTNG_VIEWER_INDEX_RETRY
:
735 printf_verbose("get_next_index: retry\n");
738 case LTTNG_VIEWER_INDEX_HUP
:
739 printf_verbose("get_next_index: stream hung up\n");
740 viewer_stream
->id
= -1ULL;
741 viewer_stream
->fd
= -1;
744 case LTTNG_VIEWER_INDEX_ERR
:
745 fprintf(stderr
, "[error] get_next_index: error\n");
749 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
760 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
763 struct ctf_stream_pos
*pos
;
764 struct ctf_file_stream
*file_stream
;
765 struct packet_index packet_index
;
766 struct lttng_live_viewer_stream
*viewer_stream
;
767 struct lttng_live_session
*session
;
771 pos
= ctf_pos(stream_pos
);
772 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
773 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
774 session
= viewer_stream
->session
;
776 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
777 ret
= get_next_index(session
->ctx
, viewer_stream
, &packet_index
);
780 fprintf(stderr
, "[error] get_next_index failed\n");
784 pos
->packet_size
= packet_index
.packet_size
;
785 pos
->content_size
= packet_index
.content_size
;
786 pos
->mmap_base_offset
= 0;
787 if (packet_index
.offset
== EOF
) {
793 if (packet_index
.content_size
== 0) {
794 file_stream
->parent
.cycles_timestamp
= packet_index
.timestamp_end
;
795 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
796 &file_stream
->parent
, packet_index
.timestamp_end
);
798 file_stream
->parent
.cycles_timestamp
= packet_index
.timestamp_begin
;
799 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
800 &file_stream
->parent
, packet_index
.timestamp_begin
);
803 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
807 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
809 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
810 be64toh(packet_index
.offset
),
811 packet_index
.packet_size
/ CHAR_BIT
);
814 } else if (ret
< 0) {
816 fprintf(stderr
, "[error] get_data_packet failed\n");
820 printf_verbose("Index received : packet_size : %" PRIu64
821 ", offset %" PRIu64
", content_size %" PRIu64
822 ", timestamp_end : %" PRIu64
"\n",
823 packet_index
.packet_size
, packet_index
.offset
,
824 packet_index
.content_size
, packet_index
.timestamp_end
);
826 /* update trace_packet_header and stream_packet_context */
827 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
828 /* Read packet header */
829 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
832 fprintf(stderr
, "[error] trace packet header read failed\n");
836 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
837 /* Read packet context */
838 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
841 fprintf(stderr
, "[error] stream packet context read failed\n");
845 pos
->data_offset
= pos
->offset
;
851 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
853 struct bt_context
*bt_ctx
= user_data
;
854 struct lttng_live_ctf_trace
*trace
= value
;
857 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
859 fprintf(stderr
, "[error] removing trace from context\n");
861 /* remove the key/value pair from the HT. */
865 static void add_traces(gpointer key
, gpointer value
, gpointer user_data
)
867 int i
, ret
, total_metadata
= 0;
868 uint64_t metadata_len
;
869 struct bt_context
*bt_ctx
= user_data
;
870 struct lttng_live_ctf_trace
*trace
= value
;
871 struct lttng_live_viewer_stream
*stream
;
872 struct bt_mmap_stream
*new_mmap_stream
;
873 struct bt_mmap_stream_list mmap_list
;
874 struct lttng_live_ctx
*ctx
= NULL
;
876 BT_INIT_LIST_HEAD(&mmap_list
.head
);
878 for (i
= 0; i
< trace
->streams
->len
; i
++) {
879 stream
= g_ptr_array_index(trace
->streams
, i
);
880 ctx
= stream
->session
->ctx
;
882 if (!stream
->metadata_flag
) {
883 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
884 new_mmap_stream
->priv
= (void *) stream
;
885 new_mmap_stream
->fd
= -1;
886 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
888 /* Get all possible metadata before starting */
890 ret
= get_new_metadata(ctx
, stream
,
893 total_metadata
+= metadata_len
;
895 } while (ret
== 0 || total_metadata
== 0);
896 trace
->metadata_fp
= fopen(stream
->path
, "r");
900 if (!trace
->metadata_fp
) {
901 fprintf(stderr
, "[error] No metadata stream opened\n");
905 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
906 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
908 fprintf(stderr
, "[error] Error adding trace\n");
911 trace
->trace_id
= ret
;
916 bt_context_put(bt_ctx
);
921 void lttng_live_read(struct lttng_live_ctx
*ctx
, uint64_t session_id
)
923 int ret
, active_session
= 0;
924 struct bt_context
*bt_ctx
;
925 struct bt_ctf_iter
*iter
;
926 const struct bt_ctf_event
*event
;
927 struct bt_iter_pos begin_pos
;
928 struct bt_trace_descriptor
*td_write
;
929 struct bt_format
*fmt_write
;
930 struct ctf_text_stream_pos
*sout
;
932 bt_ctx
= bt_context_create();
934 fprintf(stderr
, "[error] bt_context_create allocation\n");
938 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
940 fprintf(stderr
, "[error] ctf-text error\n");
944 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
946 fprintf(stderr
, "[error] Error opening output trace\n");
950 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
952 if (!sout
->parent
.event_cb
)
956 * As long as the session is active, we try to reattach to it,
957 * even if all the streams get closed.
963 ret
= lttng_live_attach_session(ctx
, session_id
);
964 printf_verbose("Attaching session returns %d\n", ret
);
966 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
969 fprintf(stderr
, "[error] Unknown "
976 } while (ctx
->session
->stream_count
== 0);
978 g_hash_table_foreach(ctx
->session
->ctf_traces
, add_traces
, bt_ctx
);
980 begin_pos
.type
= BT_SEEK_BEGIN
;
981 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
983 fprintf(stderr
, "[error] Iterator creation error\n");
987 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
988 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
993 ret
= sout
->parent
.event_cb(&sout
->parent
,
994 event
->parent
->stream
);
996 fprintf(stderr
, "[error] Writing "
1001 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1006 bt_ctf_iter_destroy(iter
);
1007 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
, del_traces
, bt_ctx
);
1008 } while (active_session
);
1011 bt_context_put(bt_ctx
);